Transferring the specified table to the database.
Used SqlBulkCopy.
The fastest and most economical way to move large amounts of data in the database.
Namespace: Nemiro.Data.SqlAssembly: Nemiro.Data (in Nemiro.Data.dll) Version: 2.11.4.126 (2.11.4.126)
public void CopyTableToServer(
DataTable value
)
public void CopyTableToServer(
DataTable value
)
Public Sub CopyTableToServer (
value As DataTable
)
Public Sub CopyTableToServer (
value As DataTable
)
Parameters
- value
- Type: System.Data DataTable
Table whose you want transfer to the database.
Exception | Condition |
---|
SqlException | The exception that is thrown when SQL Server returns a warning or error. |
StringOrBinaryDataWouldBeTruncatedException | An exception occurs if some of the fields of the table are added values that exceed the allowable size of a table field. |
Target table name should be in the property TableName instance of DataTable.
Fields list of instance the DataTable class must match to columns of the target table.
Caching options are ignored.
using (SqlClient client = new SqlClient())
{
DataTable table = new DataTable("hotels");
table.Columns.Add("hotel_code");
table.Columns.Add("hotel_name");
table.Columns.Add("hotel_stars");
table.Columns.Add("date_created");
Random rnd = new Random(DateTime.Now.Millisecond);
for (int i = 0; i <= 1000; i++)
{
table.Rows.Add(Guid.NewGuid().ToString().Substring(0, 4), Guid.NewGuid().ToString().Replace("-", ""), rnd.Next(1, 5), DateTime.Now);
}
client.CopyTableToServer(table);
}
using (SqlClient client = new SqlClient())
{
// Create DataTable instance.
// Table name is [hotels].
// In SQL Server database should have a table named [hotels].
DataTable table = new DataTable("hotels");
// Add fields in the table.
table.Columns.Add("hotel_code");
table.Columns.Add("hotel_name");
table.Columns.Add("hotel_stars");
table.Columns.Add("date_created");
// Random data generator, for example.
Random rnd = new Random(DateTime.Now.Millisecond);
for (int i = 0; i <= 1000; i++)
{
table.Rows.Add(Guid.NewGuid().ToString().Substring(0, 4), Guid.NewGuid().ToString().Replace("-", ""), rnd.Next(1, 5), DateTime.Now);
}
// Transfer data to SQL Server
client.CopyTableToServer(table);
}
Using client As New SqlClient()
Dim table As New DataTable("hotels")
table.Columns.Add("hotel_code")
table.Columns.Add("hotel_name")
table.Columns.Add("hotel_stars")
table.Columns.Add("date_created")
Dim rnd As New Random(DateTime.Now.Millisecond)
For i As Integer = 0 To 1000
table.Rows.Add(Guid.NewGuid().ToString().Substring(0, 4), Guid.NewGuid().ToString().Replace("-", ""), rnd.Next(1, 5), DateTime.Now)
Next
client.CopyTableToServer(table)
End Using
Using client As New SqlClient()
' Create DataTable instance.
' Table name is [hotels].
' In SQL Server database should have a table named [hotels].
Dim table As New DataTable("hotels")
' Add fields in the table.
table.Columns.Add("hotel_code")
table.Columns.Add("hotel_name")
table.Columns.Add("hotel_stars")
table.Columns.Add("date_created")
' Random data generator, for example.
Dim rnd As New Random(DateTime.Now.Millisecond)
For i As Integer = 0 To 1000
table.Rows.Add(Guid.NewGuid().ToString().Substring(0, 4), Guid.NewGuid().ToString().Replace("-", ""), rnd.Next(1, 5), DateTime.Now)
Next
' Transfer data to SQL Server
client.CopyTableToServer(table)
End Using