D_D*_*D_D 7 c# t-sql datatable
无论如何使用存储过程将C#中的System.Data.DataTable批量插入到SQL Server表中并将此表作为参数传递?
该表没有固定数量的记录.
是的有一种方法:
DataTable dataTable = null; // your data needs to be here
try
{
ConnectionStringSettings mConString = ConfigurationManager.ConnectionStrings["SiteSqlServer"];
// Optional truncating old table
using (SqlConnection connection = new SqlConnection(mConString.ConnectionString))
{
connection.Open();
// Delete old entries
SqlCommand truncate = new SqlCommand("TRUNCATE TABLE MYTABLE", connection);
truncate.ExecuteNonQuery();
}
SqlBulkCopy bulkCopy = new SqlBulkCopy(mConString.ConnectionString, SqlBulkCopyOptions.TableLock)
{
DestinationTableName = "dbo.MYTABLE",
BatchSize = 100000,
BulkCopyTimeout = 360
};
bulkCopy.WriteToServer(dataTable);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
Run Code Online (Sandbox Code Playgroud)
请体验BatchSize - 100000对我有好处 - 它不应该大于此 - 否则速度迁移减少.BatchSize不限制您的数据 - 它只是将发送到sql server的每个"数据包"的大小.
SiteSQLServer应位于app.config或web.config中.如果没有,您需要在此处更改参数.
请将MYTABLE更改为您的表名.