快速插入大量数据的asp.net代码(Sqlserver)
前端之家收集整理的这篇文章主要介绍了
快速插入大量数据的asp.net代码(Sqlserver),
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
<div class="codetitle"><a style="CURSOR: pointer" data="74415" class="copybut" id="copybut74415" onclick="doCopy('code74415')"> 代码如下:
<div class="codebody" id="code74415">
using System.Data;
using System.Diagnostics;
using System.Data.
sqlClient;
string connectionString = "Data Source=HG-J3EJJ9LSW5PY;Initial Catalog=Test;User ID=sa;password=hg";
DataTable dataTable =
sql_.select_datagrid(" select a from large where 1=0 ").Tables[0];
string passportKey;
for (int i = 0; i < 100000; i++)
{
passportKey = Guid.NewGuid().ToString();
DataRow dataRow = dataTable.NewRow();
dataRow[0] = passportKey;
dataTable.Rows.Add(dataRow);
}
sqlConnection
sqlConnection = new
sqlConnection(connectionString);
sqlConnection.Open();
sqlTransaction
sqltran =
sqlConnection.BeginTransaction();
sqlBulkCopy
sqlBulkCopy = new
sqlBulkCopy(
sqlConnection,
sqlBulkCopyOptions.KeepIdentity,
sqltran);
sqlBulkCopy.DestinationTableName = "large";
sqlBulkCopy.BatchSize = dataTable.Rows.Count;
if (dataTable != null && dataTable.Rows.Count != 0)
{
sqlBulkCopy.WriteToServer(dataTable);
}
sqlBulkCopy.Close();
sqltran.Rollback();
sqlConnection.Close();