ASP.NET 向SQLSERVER中批量插入数据

 

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();

你可能感兴趣的:(sql,String,datagrid,null,asp.net,sqlserver)