public void Executebuklcopy (DataTable dt)
{
SqlConnection conn = new SqlConnection (connectionString);
if (dt = = NULL | | dt. Rows.Count <= 0 | | String.IsNullOrEmpty (dt. TableName))
{return;}
IF (Conn. State = ConnectionState.Open)
{
Conn. Open ();
}
SqlTransaction sqlbulktransaction = conn. BeginTransaction ();
Check the constraints while inserting the data, and if an error occurs, call the Sqlbulktransaction transaction
SqlBulkCopy copy = new SqlBulkCopy (conn, sqlbulkcopyoptions.checkconstraints, sqlbulktransaction);
Copy. DestinationTableName = dt. TableName;
foreach (DataColumn dc in dt. Columns)
{
Copy. Columnmappings.add (DC. ColumnName, DC. ColumnName);
}
Try
{
10,000 strips per time
if (dt. Rows.Count > 10000)
{
system.collections.generic.list<datarow> _list = new system.collections.generic.list<datarow> ();
int i = 0;
for (i = 0; i < dt. Rows.Count; i++)
{
if (i > 0 && i% 10000 = = 0)
{
Copy. WriteToServer (_list. ToArray ());
Sqlbulktransaction.commit ();
_list. Clear ();
}
Else
{
_list. ADD (dt. Rows[i]);
}
}
if (_list. Count > 0)
{
Copy. WriteToServer (_list. ToArray ());
Sqlbulktransaction.commit ();
}
}
Else
{
Copy. WriteToServer (DT);
Sqlbulktransaction.commit ();
}
}
catch (Exception ex)
{
Sqlbulktransaction.rollback ();
Console.WriteLine (ex. ToString ());
}
Finally
{
Copy. Close ();
Conn. Close ();
}
}
Big Data BULK INSERT Database