必胜高考网 > 计算机类 > 计算机等级 > 资讯 >

三级:使用BulkCopy将大量数据复制到数据库

时间: 家辉2 资讯

  CREATE TABLE TEMP_ORDERS

  (

  TEMP_ORDERID INT,

  TEMP_CUSTOMERID NCHAR(5),

  TEMP_ORDERDATE DATETIME,

  TEMP_SHIPNAME NVARCHAR(40)

  )

  下面为模拟程序

  protected void Page_Load(object sender, EventArgs e)

  {

  #region 从NorthWind的Orders表获取要插入的数据

  DataTable dtNorthWindOrders = new DataTable();

  using ( SqlConnection northWindConnection = new SqlConnection( "Data Source=.;Initial Catalog=NorthWind;Integrated Security=True" ) )

  {

  using ( SqlDataAdapter northWindAdapter = new SqlDataAdapter( "SELECT ORDERID,CUSTOMERID,ORDERDATE,SHIPNAME FROM ORDERS" , northWindConnection ) )

  {

  northWindAdapter.Fill( dtNorthWindOrders );

  }

  }

  #endregion

  using ( SqlConnection tempdbConnection = new SqlConnection( "Data Source=.;Initial Catalog=Tempdb;Integrated Security=True" ) )

  {

  tempdbConnection.Open( );

  using ( SqlTransaction tran = tempdbConnection.BeginTransaction( ) )

  {

  SqlBulkCopy bulkCopyOrders = new SqlBulkCopy( tempdbConnection , SqlBulkCopyOptions.Default , tran );

  bulkCopyOrders.DestinationTableName = "TEMP_ORDERS";

  //将数据源表字段和目标表的字段做个映射

  bulkCopyOrders.ColumnMappings.Add( "ORDERID" , "TEMP_ORDERID" );

  bulkCopyOrders.ColumnMappings.Add( "CUSTOMERID" , "TEMP_CUSTOMERID" );

  bulkCopyOrders.ColumnMappings.Add( "ORDERDATE" , "TEMP_ORDERDATE" );

  bulkCopyOrders.ColumnMappings.Add( "SHIPNAME" , "TEMP_SHIPNAME" );

  bulkCopyOrders.BulkCopyTimeout = 1000;

  //每处理10行触发一个事件向页面上输出一个消息

  bulkCopyOrders.SqlRowsCopied += new SqlRowsCopiedEventHandler( onRowsCopy );

  bulkCopyOrders.NotifyAfter = 10;

  try

  {

  bulkCopyOrders.WriteToServer( dtNorthWindOrders );

  tran.Commit( );

  }

  catch ( Exception ex )

  {

  Response.Write( ex.ToString( ) );

  }

  finally

  {

  dtNorthWindOrders = null;

  }

  }

  }

  }

  private void onRowsCopy ( object Sender , SqlRowsCopiedEventArgs args )

  {

  Response.Write("已复制:"+ args.RowsCopied.ToString( ) + "" );

  }

  通过SQL SERVER 事件探察器发现执行的SQL为:

  insert bulk TEMP_ORDERS ([TEMP_ORDERID] Int, [TEMP_CUSTOMERID] NChar(5) COLLATE Chinese_PRC_CI_AS, [TEMP_ORDERDATE] DateTime, [TEMP_SHIPNAME] NVarChar(40) COLLATE Chinese_PRC_CI_AS)

  通过运行程序可以看出这个速度是相当的快 , 使用这个方法的最大优点是 : 减少对数据库的访问次数 .

  WriteToServer不仅可以处理 DataTable 对象 , 还可以处理 DataReader , DataRow 对象数组。

56095