private void AddBulkInsert(string tableName, DataTable dt)
{
string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["XzyConnstring"].ToString();
using (SqlConnection destinationConnection =
new SqlConnection(connectionString))
{
destinationConnection.Open();
using (SqlBulkCopy bulkCopy =
new SqlBulkCopy(destinationConnection))
{
bulkCopy.DestinationTableName = tableName;
try
{
for (int i = 0; i < dt.Columns.Count; i++)
{
bulkCopy.ColumnMappings.Add(dt.Columns[i].ColumnName, dt.Columns[i].ColumnName);
}
bulkCopy.WriteToServer(dt);
}
catch (Exception ex)
{
return;
}
}
}
}
原文地址:https://www.cnblogs.com/moondd22/p/9212368.html