当前位置:编程学习 > C#/ASP.NET >>

asp.net 快速向mssql server插入大量数据


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();
补充:asp.net教程,.Net开发
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,