如何快速插入大量的数据(Sqlserver)
经过测试 10万条记录 需要1.5-1.7秒
只是插入一列 4列的话 需要3秒 (我的机器是4G内存)
目标数据库只能是Sqlserver
来源数据库 无所谓 只要能用ado.net 将来源数据读取到Dataset或者Datareader 中就可以
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();
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();
注解: <1> sqlBulkCopy.DestinationTableName = "large"; large 指的是目标表的名称
<2> DataTable 的结构要和数据库中的表的结构相同
(DataTable的列不能多于数据库里面的)
(DataTable的列可以少于数据库里面的 如果数据库这一列有默认值的话)
<3> 这里面 我使用了事务 您在使用的时候 也可以不用事务