您的位置:首页 > 博客中心 > 数据库 >

ADO.NET 新特性之SqlBulkCopy

时间:2022-03-16 10:00

在.Net1.1中无论是对于批量插入整个DataTable中的所有数据到数据库中,还是进行不同数据源之间的迁移,都不是很方便。而 在.Net2.0中,SQLClient命名空间下增加了几个新类帮助我们通过DataTable或DataReader批量迁移数据。数据源可以来自关 系数据库或者XML文件,甚至WebService返回结果。其中最重要的一个类就是SqlBulkCopy类,使用它可以很方便的帮助我们把数据源的数 据迁移到目标数据库中。
下面我们先通过一个简单的例子说明这个类的使用:

首先:web.config

gxlsystem.com,布布扣 <connectionStrings>
gxlsystem.com,布布扣    <add name="srcDBConnection" connectionString="server=.;database=pubs;uid=sa;pwd="/>
gxlsystem.com,布布扣    <add name="desDBConnection" connectionString="server=.;database=NorthWind;uid=sa;pwd="/>
gxlsystem.com,布布扣  </connectionStrings>

C#文件: 前台不Copy了,就一个按钮,一个Label

gxlsystem.com,布布扣using System;
gxlsystem.com,布布扣using System.Data;
gxlsystem.com,布布扣using System.Configuration;
gxlsystem.com,布布扣using System.Collections;
gxlsystem.com,布布扣using System.Web;
gxlsystem.com,布布扣using System.Web.Security;
gxlsystem.com,布布扣using System.Web.UI;
gxlsystem.com,布布扣using System.Web.UI.WebControls;
gxlsystem.com,布布扣using System.Web.UI.WebControls.WebParts;
gxlsystem.com,布布扣using System.Web.UI.HtmlControls;
gxlsystem.com,布布扣using System.Data.SqlClient;
gxlsystem.com,布布扣
gxlsystem.com,布布扣public partial class ASP_NET : System.Web.UI.Page
gxlsystem.com,布布扣{
gxlsystem.com,布布扣    private DateTime startTime;
gxlsystem.com,布布扣
gxlsystem.com,布布扣    protected void Button1_Click(object sender, EventArgs e)
gxlsystem.com,布布扣    {
gxlsystem.com,布布扣        startTime = DateTime.Now;
gxlsystem.com,布布扣        string srcConnString = "";
gxlsystem.com,布布扣        string desConnString = "";
gxlsystem.com,布布扣        SqlConnection srcConnection = new SqlConnection();
gxlsystem.com,布布扣        SqlConnection desConnection = new SqlConnection();
gxlsystem.com,布布扣        SqlCommand sqlcmd = new SqlCommand();
gxlsystem.com,布布扣        SqlDataAdapter da = new SqlDataAdapter();
gxlsystem.com,布布扣        DataTable dt = new DataTable();
gxlsystem.com,布布扣        //srcConnString = ConfigurationManager.ConnectionStrings["srcDBConnection"].ConnectionString;
gxlsystem.com,布布扣        desConnString = ConfigurationManager.ConnectionStrings["desDBConnection"].ToString();
gxlsystem.com,布布扣        //srcConnection.ConnectionString = srcConnString;
gxlsystem.com,布布扣        srcConnection.ConnectionString = desConnString;
gxlsystem.com,布布扣        sqlcmd.Connection = srcConnection;
gxlsystem.com,布布扣        //sqlcmd.CommandText = "select * from jobs";
gxlsystem.com,布布扣        sqlcmd.CommandText = "select * from abc";
gxlsystem.com,布布扣        sqlcmd.CommandType = CommandType.Text;
gxlsystem.com,布布扣        sqlcmd.Connection.Open();
gxlsystem.com,布布扣        da.SelectCommand = sqlcmd;
gxlsystem.com,布布扣        da.Fill(dt);
gxlsystem.com,布布扣
gxlsystem.com,布布扣        SqlBulkCopy sbc = new SqlBulkCopy(desConnString,SqlBulkCopyOptions.UseInternalTransaction);
gxlsystem.com,布布扣        sbc.BulkCopyTimeout = 5000;
gxlsystem.com,布布扣        sbc.SqlRowsCopied +=new SqlRowsCopiedEventHandler(OnRowsCopied);
gxlsystem.com,布布扣        sbc.NotifyAfter = dt.Rows.Count;
gxlsystem.com,布布扣
gxlsystem.com,布布扣        try
gxlsystem.com,布布扣        {
gxlsystem.com,布布扣           // sbc.DestinationTableName = "jobs";
gxlsystem.com,布布扣            sbc.DestinationTableName = "bcd";
gxlsystem.com,布布扣            sbc.WriteToServer(dt);
gxlsystem.com,布布扣        }
gxlsystem.com,布布扣        catch (Exception ex)
gxlsystem.com,布布扣        {
gxlsystem.com,布布扣            lblCounter.Text = ex.Message.ToString();
gxlsystem.com,布布扣        }
gxlsystem.com,布布扣        finally
gxlsystem.com,布布扣        {
gxlsystem.com,布布扣            sqlcmd.Clone();
gxlsystem.com,布布扣            srcConnection.Close();
gxlsystem.com,布布扣            desConnection.Close();
gxlsystem.com,布布扣            
gxlsystem.com,布布扣        }
gxlsystem.com,布布扣
gxlsystem.com,布布扣    }
gxlsystem.com,布布扣    private void OnRowsCopied(object sender, SqlRowsCopiedEventArgs args)
gxlsystem.com,布布扣    {
gxlsystem.com,布布扣        lblCounter.Text += args.RowsCopied.ToString() + " rows are copied<Br>";
gxlsystem.com,布布扣        TimeSpan copyTime = DateTime.Now - startTime;
gxlsystem.com,布布扣        lblCounter.Text += "Copy Time:" + copyTime.Seconds.ToString() + "." + copyTime.Milliseconds.ToString() + " seconds";
gxlsystem.com,布布扣    }
gxlsystem.com,布布扣}
gxlsystem.com,布布扣

代码分析:

gxlsystem.com,布布扣SqlBulkCopy sbc = new SqlBulkCopy(desConnString,SqlBulkCopyOptions.UseInternalTransaction);
gxlsystem.com,布布扣先生成SqlBulkCopy 实例,构造函数指定了目标数据库,使用SqlBulkCopyOptions.UseInternalTransaction是指迁移动作指定在一个Transaction当中,如果数据迁移中产生错误或异常将发生回滚。
gxlsystem.com,布布扣

sbc.BulkCopyTimeout = 5000000;    //指定操作完成的Timeout时间

gxlsystem.com,布布扣 sbc.SqlRowsCopied +=new SqlRowsCopiedEventHandler(OnRowsCopied);
gxlsystem.com,布布扣  sbc.NotifyAfter = dt.Rows.Count;
gxlsystem.com,布布扣
gxlsystem.com,布布扣        try
gxlsystem.com,布布扣        {
gxlsystem.com,布布扣           // sbc.DestinationTableName = "jobs";
gxlsystem.com,布布扣            sbc.DestinationTableName = "bcd";
gxlsystem.com,布布扣            sbc.WriteToServer(dt);
gxlsystem.com,布布扣        }
gxlsystem.com,布布扣NotifyAfter 属性指定通知通知事件前处理的数据行数,在这里指定为表的行数,并添加SqlRowsCopied事件输出整个迁移过程的时间。 WriteToServer方法就是将数据源拷备到目标数据库。在使用WriteToServer方法之前必须先指定 DestinationTableName属性,也就是目标数据库的表名,
gxlsystem.com,布布扣

性能方面:我在Sql中用proc插入68万条数据花了近8分钟,用SqlBulkCopy花了53.234秒~,效率高了7倍耶!不过现在也不做这方面的底层了,呵呵,把自己写的一个测试存储过程也贴上吧,方便自己学习

gxlsystem.com,布布扣create table abc
gxlsystem.com,布布扣(
gxlsystem.com,布布扣  aid int identity(1,1) primary key,
gxlsystem.com,布布扣  adesc varchar(50) not null
gxlsystem.com,布布扣)
gxlsystem.com,布布扣go
gxlsystem.com,布布扣
gxlsystem.com,布布扣/**********存储过程**********************/
gxlsystem.com,布布扣create proc addData
gxlsystem.com,布布扣as
gxlsystem.com,布布扣declare @i int
gxlsystem.com,布布扣set @i=1
gxlsystem.com,布布扣while @i < 1000000
gxlsystem.com,布布扣begin
gxlsystem.com,布布扣insert into abc values (‘testDescription‘)
gxlsystem.com,布布扣set @i = @i + 1
gxlsystem.com,布布扣end
gxlsystem.com,布布扣go
gxlsystem.com,布布扣
gxlsystem.com,布布扣select * into titles from pubs.dbo.titles where  1> 3 复制跨数据库的表结构

ADO.NET 新特性之SqlBulkCopy,布布扣,bubuko.com

热门排行

今日推荐

热门手游