大家好,又见面了,我是你们的朋友全栈君。
Microsoft SQL Server 包含名为 bcp 的常用命令行应用程序,用于快速将大文件批量复制到 SQL Server 数据库的表或视图中。使用 SqlBulkCopy 类可以编写提供类似功能的托管代码解决方案。还可以通过其他方式将数据加载到 SQL Server 表中(例如 INSERT 语句),但是 SqlBulkCopy 提供的性能要明显优于这些方式。
SqlBulkCopy 类只能用于向 SQL Server 表中写入数据。但是,数据源不限于 SQL Server;可以使用任何数据源,只要数据可以加载到 DataTable 实例或使用 IDataReader 实例读取即可。
使用 SqlBulkCopy 类可以执行下列操作:
-
单次批量复制操作
-
多次批量复制操作
-
事务中的批量复制操作
示例1: 单次批量复制操作
private
void
sqlBulkCopy()
{
//建立连接信息
SqlConnectionStringBuilder sqlcsb = new SqlConnectionStringBuilder();
//sqlcsb.DataSource = “SQLProduction”;
//sqlcsb.InitialCatalog = “Sales”;
//sqlcsb.IntegratedSecurity = true;
sqlcsb.ConnectionString = @”data source=myDBServer;user id=sa;initial catalog=myDB;password=“;
SqlConnection SqlConn = new SqlConnection(sqlcsb.ConnectionString);
//建立获取数据命令
//SqlCommand cmd = new SqlCommand(“SELECT * FROM SG4262010100007”, cnn);
//SqlConn.Open();
//SqlDataReader dataReader = cmd.ExecuteReader();
string dbFile = @”C:/20071214.mdb“;
OleDbConnectionStringBuilder oleConStr = new OleDbConnectionStringBuilder();
oleConStr.ConnectionString = “Provider=Microsoft.Jet.OLEDB.4.0;Data Source=“ + dbFile +
“;User Id=admin;Password=;“;
OleDbConnection OleConn = new OleDbConnection(oleConStr.ConnectionString);
OleDbCommand cmd = new OleDbCommand(“select * from SG4262010100007 “, OleConn);
OleConn.Open();
System.Data.OleDb.OleDbDataReader dataReader = cmd.ExecuteReader();
SqlConn.Open();
SqlTransaction transaction = SqlConn.BeginTransaction(); ;
// 创建一个 SqlBulkCopy 对象.
SqlBulkCopy bulkCopy = new SqlBulkCopy(SqlConn, SqlBulkCopyOptions.KeepIdentity, transaction);
bulkCopy.BatchSize = 10;
bulkCopy.BulkCopyTimeout = 500000000;
bulkCopy.SqlRowsCopied += new SqlRowsCopiedEventHandler(sbc_SqlRowsCopied);
bulkCopy.NotifyAfter = 10;
//复制数据至目标对象
bulkCopy.DestinationTableName = “SG4262010100007“;
try
{
bulkCopy.WriteToServer(dataReader);
transaction.Commit();
}
catch (Exception err)
{
transaction.Rollback();
throw new Exception(err.ToString());
}
// 关闭所有的对象
bulkCopy.Close();
dataReader.Close();
OleConn.Close();
SqlConn.Close();
}
void
sbc_SqlRowsCopied(
object
sender, SqlRowsCopiedEventArgs e)
{
showMsg(“已完成行数:“ + e.RowsCopied.ToString());
}
private
delegate
void
showMsgDelegate(
string
msg);
private
void
showMsgDel(
string
msg)
{
this.myRichTextBox1.AppendText(msg + System.Environment.NewLine);
}
private
void
showMsg(
string
msg)
{
this.BeginInvoke(new showMsgDelegate(showMsgDel), new object[] { msg });
}
2.
我们可以有选择性的从目标数据选取数据大批量复制至目标对象指定的字段中.
private
void
HasSelSqlBulkCopy()
{
//建立连接信息
SqlConnectionStringBuilder sqlcsb = new SqlConnectionStringBuilder();
//sqlcsb.DataSource = “SQLProduction”;
//sqlcsb.InitialCatalog = “Sales”;
//sqlcsb.IntegratedSecurity = true;
sqlcsb.ConnectionString = @”data source=myDBServer;user id=sa;initial catalog=myDB;password=“;
SqlConnection SqlConn = new SqlConnection(sqlcsb.ConnectionString);
//建立获取数据命令
//SqlCommand cmd = new SqlCommand(“SELECT * FROM SG4262010100007”, cnn);
//SqlConn.Open();
//SqlDataReader dataReader = cmd.ExecuteReader();
string dbFile = @”C:/20071214.mdb“;
OleDbConnectionStringBuilder oleConStr = new OleDbConnectionStringBuilder();
oleConStr.ConnectionString = “Provider=Microsoft.Jet.OLEDB.4.0;Data Source=“ + dbFile +
“;User Id=admin;Password=;“;
OleDbConnection OleConn = new OleDbConnection(oleConStr.ConnectionString);
OleDbCommand cmd = new OleDbCommand(“select DATETIME,INFOTAG,MSGTEXT from SG4262010100007 “, OleConn);
OleConn.Open();
System.Data.OleDb.OleDbDataReader dataReader = cmd.ExecuteReader();
SqlConn.Open();
SqlTransaction transaction = SqlConn.BeginTransaction(); ;
// 创建一个 SqlBulkCopy 对象.
SqlBulkCopy bulkCopy = new SqlBulkCopy(SqlConn, SqlBulkCopyOptions.KeepIdentity, transaction);
bulkCopy.BatchSize = 10;
bulkCopy.BulkCopyTimeout = 500000000;
bulkCopy.SqlRowsCopied += new SqlRowsCopiedEventHandler(sbc_SqlRowsCopied);
bulkCopy.NotifyAfter = 10;
//复制数据至目标对象
bulkCopy.DestinationTableName = “testtb“;
bulkCopy.ColumnMappings.Clear();
//设置源对象与目标对象的字段对应关系
SqlBulkCopyColumnMapping column1 =
new SqlBulkCopyColumnMapping(
“DATETIME“, “date“);
bulkCopy.ColumnMappings.Add(column1);
SqlBulkCopyColumnMapping column2 =
new SqlBulkCopyColumnMapping(
“INFOTAG“, “flag“);
bulkCopy.ColumnMappings.Add(column2);
SqlBulkCopyColumnMapping
column3 =
new SqlBulkCopyColumnMapping(
“MSGTEXT“, “myValue“);
bulkCopy.ColumnMappings.Add(column3);
try
{
bulkCopy.WriteToServer(dataReader);
transaction.Commit();
}
catch (Exception err)
{
transaction.Rollback();
throw new Exception(err.ToString());
}
// 关闭所有的对象
bulkCopy.Close();
dataReader.Close();
OleConn.Close();
SqlConn.Close();
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/140657.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...