详解C#批量插入数据到Sqlserver中的四种方式

2019-12-30 15:13:05刘景俊

方式三:INSERT INTO xx select...


 INSERT INTO Product(Id,Name,Price)
 SELECT NEWID(),'牛栏1段',160 
 UNION ALL 
 SELECT NEWID(),'牛栏2段',180
 UNION ALL
......

方式四:拼接SQL


INSERT INTO Product(Id,Name,Price) VALUES
(newid(),'牛栏1段',160)
,(newid(),'牛栏2段',260)
......

在C#中通过ADO.NET来实现批量操作存在四种与之对应的方式。

方式一:逐条插入


#region 方式一
 static void InsertOne()
 {
  Console.WriteLine("采用一条一条插入的方式实现");
  Stopwatch sw = new Stopwatch();
  using (SqlConnection conn = new SqlConnection(StrConnMsg)) //using中会自动Open和Close 连接。
  {
  string sql = "INSERT INTO Product(Id,Name,Price) VALUES(newid(),@p,@d)";
  conn.Open();
  for (int i = 0; i < totalRow; i++)
  {
   using (SqlCommand cmd = new SqlCommand(sql, conn))
   {
   cmd.Parameters.AddWithValue("@p", "商品" + i);
   cmd.Parameters.AddWithValue("@d", i);
   sw.Start();
   cmd.ExecuteNonQuery();
   Console.WriteLine(string.Format("插入一条记录,已耗时{0}毫秒", sw.ElapsedMilliseconds));
   }
   if (i == getRow)
   {
   sw.Stop();
   break;
   }
  }
  }
  Console.WriteLine(string.Format("插入{0}条记录,每{4}条的插入时间是{1}毫秒,预估总得插入时间是{2}毫秒,{3}分钟",
 totalRow, sw.ElapsedMilliseconds, ((sw.ElapsedMilliseconds / getRow) * totalRow), GetMinute((sw.ElapsedMilliseconds / getRow * totalRow)), getRow));
 }
 static int GetMinute(long l)
 {
  return (Int32)l / 60000;
 } 
 #endregion

运行结果如下:

C#,批量插入数据

我们会发现插入100w条记录,预计需要50分钟时间,每插入一条记录大概需要3毫秒左右。

方式二:使用SqlBulk


#region 方式二
 static void InsertTwo()
 {
  Console.WriteLine("使用Bulk插入的实现方式");
  Stopwatch sw = new Stopwatch();
  DataTable dt = GetTableSchema(); 
  using (SqlConnection conn = new SqlConnection(StrConnMsg))
  {
  SqlBulkCopy bulkCopy = new SqlBulkCopy(conn);
  bulkCopy.DestinationTableName = "Product";
  bulkCopy.BatchSize = dt.Rows.Count;
  conn.Open();
  sw.Start();
  for (int i = 0; i < totalRow;i++ )
  {
   DataRow dr = dt.NewRow();
   dr[0] = Guid.NewGuid();
   dr[1] = string.Format("商品", i);
   dr[2] = (decimal)i;
   dt.Rows.Add(dr);
  }
   if (dt != null && dt.Rows.Count != 0)
   {
   bulkCopy.WriteToServer(dt);
   sw.Stop();
   }
   Console.WriteLine(string.Format("插入{0}条记录共花费{1}毫秒,{2}分钟", totalRow, sw.ElapsedMilliseconds, GetMinute(sw.ElapsedMilliseconds)));
  }
 }
 static DataTable GetTableSchema()
 {
  DataTable dt = new DataTable();
  dt.Columns.AddRange(new DataColumn[] { 
 new DataColumn("Id",typeof(Guid)), 
 new DataColumn("Name",typeof(string)), 
 new DataColumn("Price",typeof(decimal))});
  return dt;
 }
 #endregion