.net 大数据量的批量插入

  1. 使用Insert循环插入(每次一条)

  2. 使用BulkCopy写入;

    1. 1,创建一个DataTable dt
      
      2,using (SqlBulkCopy sbc = new SqlBulkCopy(目标库连接字符串))//目标库的链接字符串
                  {
                      sbc.BulkCopyTimeout = 600;
                      sbc.BatchSize = dt.Rows.Count;
                      sbc.DestinationTableName = "目标表名";//目标表
                      sbc.WriteToServer(dt);
                  }
      }
    2. 第二种写法
                  SqlDataReader reader =commandSourceData.ExecuteReader();           
      
                  using (SqlConnection destinationConnection = new SqlConnection(connectionString))
                  {
                      destinationConnection.Open();
      
                      using (SqlBulkCopy bulkCopy =new SqlBulkCopy(destinationConnection))
                      {
                          bulkCopy.DestinationTableName = "dbo.BulkCopyDemoMatchingColumns";                    
                          try
                          {                       
                              bulkCopy.WriteToServer(reader);
                          }                    catch (Exception ex)
                          {
                              Console.WriteLine(ex.Message);
                          }                    finally
                          {                        // Close the SqlDataReader. The SqlBulkCopy
                              // object is automatically closed at the end
                              // of the using block.
                              reader.Close();
                          }
                      }               
                      long countEnd = System.Convert.ToInt32(
                          commandRowCount.ExecuteScalar());
                      Console.WriteLine("Ending row count = {0}", countEnd);
                      Console.WriteLine("{0} rows were added.", countEnd - countStart);
                      Console.WriteLine("Press Enter to finish.");
                      Console.ReadLine();
                  }
              }
  3. 使用表值参数写入。

    1.      SqlParameter[] paramters = new SqlParameter[]
                           {
                               SqlParamHelper.MakeInParam("@dt",SqlDbType.Structured)
                          };
                      DataSet ds = SqlHelper.ExecuteDataset(ComputingDB_ConnString, CommandType.StoredProcedure, "存储过程名", paramters);
      
                      string[] sqls = new string[]
                      {
                          @" insert into table1
                            select * from @dt
                          ",
                     };
      
                      using (SqlConnection connection = new SqlConnection(StatDB_ConnString))
                      {
                          connection.Open();
                          SqlTransaction trans = connection.BeginTransaction();
                          try
                          {
                              string[] typeNames = new string[] { "表类型名" };
                              for (int i = 0; i < sqls.Length; i++)
                              {
                                  paramters[0].Value = ds.Tables[i];
                                  paramters[0].TypeName = typeNames[i];
                                  SqlHelper.ExecuteNonQuery(trans, CommandType.Text, sqls[i], paramters);
                              };
                              trans.Commit();
                          }
                          catch (Exception ex)
                          {
                              trans.Rollback();
                              throw;
                          }
                      }

总结:Insert比较适合于少量数据的添加,如果是大批量的数据,只能考虑使用BulkCopy或表值参数方式,后俩者相比于前者会有一个量级的提升,随着数据量的提升这个差别会越来越大


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值