Home > Database > Mysql Tutorial > 快速插入大量数据的asp.net代码(Sqlserver)

快速插入大量数据的asp.net代码(Sqlserver)

WBOY
Release: 2016-06-07 18:01:49
Original
910 people have browsed it

目标数据库只能是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 {
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();

注解: sqlBulkCopy.DestinationTableName = "large"; large 指的是目标表的名称
DataTable 的结构要和数据库中的表的结构相同
(DataTable的列不能多于数据库里面的)
(DataTable的列可以少于数据库里面的 如果数据库这一列有默认值的话)
这里面 我使用了事务 您在使用的时候 也可以不用事务
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template