使用SqlBulkCopy类加载其他源数据到SQL表
2006-04-29 12:07 Clingingboy 阅读(6097) 评论(1) 编辑 收藏 举报MSDN摘要:Microsoft SQL Server 提供一个称为 bcp 的流行的命令提示符实用工具,用于将数据从一个表移动到另一个表(表既可以在同一个服务器上,也可以在不同服务器上)。SqlBulkCopy 类允许编写提供类似功能的托管代码解决方案。还有其他将数据加载到 SQL Server 表的方法(例如 INSERT 语句),但相比之下 SqlBulkCopy 提供明显的性能优势。
使用 SqlBulkCopy 类只能向 SQL Server 表写入数据。但是,数据源不限于 SQL Server;可以使用任何数据源,只要数据可加载到 DataTable 实例或可使用 IDataReader 实例读取数据。
protected void btnBulkCopy_Click(object sender, EventArgs e)
{
String ShajarConString =ConfigurationManager.ConnectionStrings["DSN_Shajar"].ConnectionString;
String NorthWindConString = ConfigurationManager.ConnectionStrings["DSN_Northwind"].ConnectionString;
SqlConnection ShajarCon = new SqlConnection(ShajarConString);
SqlConnection NorthwindCon = new SqlConnection(NorthWindConString);
string sql1 = " SELECT ID, First_Name, Last_Name, " +
" 'Shajar' as Source FROM MailingList_Temp ";
SqlCommand ShajarCom = new SqlCommand(sql1,ShajarCon);
SqlDataReader ShajarReader;
ShajarCon.Open();
SqlBulkCopy NorthWindBulkOp= new SqlBulkCopy(NorthWindConString, SqlBulkCopyOptions.UseInternalTransaction);
NorthWindBulkOp.DestinationTableName = "Employees";
NorthWindBulkOp.ColumnMappings.Add("Id", "EmployeeID");
NorthWindBulkOp.ColumnMappings.Add("First_Name", "FirstName");
NorthWindBulkOp.ColumnMappings.Add("Last_Name", "LastName");
SqlBulkCopyColumnMapping JobTitleColMap= new SqlBulkCopyColumnMapping("Source1", "Title");
NorthWindBulkOp.ColumnMappings.Add(JobTitleColMap);
NorthWindBulkOp.BulkCopyTimeout = 500000000;
NorthWindBulkOp.SqlRowsCopied +=
new SqlRowsCopiedEventHandler(OnRowsCopied);
NorthWindBulkOp.NotifyAfter = 1000;
ShajarReader = ShajarCom.ExecuteReader();
try
{
NorthWindBulkOp.WriteToServer(ShajarReader);
}
catch (Exception ex)
{
lblResult.Text = ex.Message;
}
finally
{
ShajarReader.Close();
}
}
private void OnRowsCopied(object sender, SqlRowsCopiedEventArgs args)
{
lblCounter.Text += args.RowsCopied.ToString() + " rows are copied<Br>";
}
{
String ShajarConString =ConfigurationManager.ConnectionStrings["DSN_Shajar"].ConnectionString;
String NorthWindConString = ConfigurationManager.ConnectionStrings["DSN_Northwind"].ConnectionString;
SqlConnection ShajarCon = new SqlConnection(ShajarConString);
SqlConnection NorthwindCon = new SqlConnection(NorthWindConString);
string sql1 = " SELECT ID, First_Name, Last_Name, " +
" 'Shajar' as Source FROM MailingList_Temp ";
SqlCommand ShajarCom = new SqlCommand(sql1,ShajarCon);
SqlDataReader ShajarReader;
ShajarCon.Open();
SqlBulkCopy NorthWindBulkOp= new SqlBulkCopy(NorthWindConString, SqlBulkCopyOptions.UseInternalTransaction);
NorthWindBulkOp.DestinationTableName = "Employees";
NorthWindBulkOp.ColumnMappings.Add("Id", "EmployeeID");
NorthWindBulkOp.ColumnMappings.Add("First_Name", "FirstName");
NorthWindBulkOp.ColumnMappings.Add("Last_Name", "LastName");
SqlBulkCopyColumnMapping JobTitleColMap= new SqlBulkCopyColumnMapping("Source1", "Title");
NorthWindBulkOp.ColumnMappings.Add(JobTitleColMap);
NorthWindBulkOp.BulkCopyTimeout = 500000000;
NorthWindBulkOp.SqlRowsCopied +=
new SqlRowsCopiedEventHandler(OnRowsCopied);
NorthWindBulkOp.NotifyAfter = 1000;
ShajarReader = ShajarCom.ExecuteReader();
try
{
NorthWindBulkOp.WriteToServer(ShajarReader);
}
catch (Exception ex)
{
lblResult.Text = ex.Message;
}
finally
{
ShajarReader.Close();
}
}
private void OnRowsCopied(object sender, SqlRowsCopiedEventArgs args)
{
lblCounter.Text += args.RowsCopied.ToString() + " rows are copied<Br>";
}
看Professional ASP.NET 2.0里的代码
首先定义一个SqlDataReader从源数据读取数据
然后定义一个SqlBulkCopy实例
构造函数参数为
- connectionString 定义将打开的且由 SqlBulkCopy 实例使用的连接的字符串。
- copyOptions SqlBulkCopyOptions 枚举中的值的组合,该枚举确定将哪些数据源行复制到目标表中。
DestinationTableName属性定义了要复制表的目标名称
ColumnMappings返回一个SqlBulkCopyColumnMapping项集合,
SqlBulkCopyColumnMappingCollection.Add 方法参数为数据源类名称,目标表中目标列的名称
也可以先定义一个SqlBulkCopyColumnMapping,然后使用Add方法添加
NotifyAfter指定生成通知事件之前要处理的行数
NorthWindBulkOp.SqlRowsCopied +=
new SqlRowsCopiedEventHandler(OnRowsCopied);
new SqlRowsCopiedEventHandler(OnRowsCopied);
指定在每次处理行完成的时候发生的事件,比如每次复制了1000行以后就提示1000行复制完成
最后WriteToServer方法,把SqlDataReader复制到指定的表中