ConnectionStr: the link string; The table in the dataTableName database; the name of the Sourcedatatable DataTable
public static void Sqlbulkcopybydatatable (String connectionstr, String datatablename, DataTable sourcedatatable, int BatchSize = 100000)
{
using (System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection (CONNECTIONSTR))
{
using (System.Data.SqlClient.SqlBulkCopy SqlBulkCopy = new System.Data.SqlClient.SqlBulkCopy (CONNECTIONSTR, System.Data.SqlClient.SqlBulkCopyOptions.UseInternalTransaction))
{
Try
{
Sqlbulkcopy.destinationtablename = dataTableName;
Sqlbulkcopy.batchsize = batchsize;
for (int i = 0; i < SourceDataTable.Columns.Count; i++)
{
SQLBULKCOPY.COLUMNMAPPINGS.ADD (Sourcedatatable.columns[i]. ColumnName, Sourcedatatable.columns[i]. ColumnName);
}
Sqlbulkcopy.writetoserver (sourcedatatable);
}
catch (Exception ex)
{
Throw ex;
}
}
}
}
Write DataTable tables to the database in batches