Class SqlBulkCopy
This is faster than individual instructions insert.
You need to increase the eigenvalue of the identification field for the primary key. To do this, first take the last value of the identifier field where you left off:
select top 1 id_customer
from customers
order by id_customer desc
Then increase the variable intwhile passing through DataSet.
Or you can instead use the GUID for the primary key column .
Sample code for using SqlBulkCopy:
public static void BulkLoadDataTable(DataTable table, string destinationTable)
{
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(_connectionString))
{
bulkCopy.BulkCopyTimeout = 1200;
bulkCopy.DestinationTableName = destinationTable;
bulkCopy.WriteToServer(table);
}
}
Strongly Printed DataTable:
using System;
using System.Data;
public class CustomersDataTable : DataTable
{
public CustomersDataTable() : base()
{
base.TableName = "Customers";
Columns.Add(new DataColumn("id_customer", typeof(int)));
Columns.Add(new DataColumn("first_name", typeof(string)));
Columns.Add(new DataColumn("last_name", typeof(string)));
Columns.Add(new DataColumn("active", typeof(bool)));
}
}
Johnb source
share