DataTable数据批量写入数据库三种方法比较

1)   insert循环插入;
2)   sqldataadapter.update(dataset,tablename);
3)   sqlbulkcopy.WriteToServer(datatable);

1、生成测试的datatable表,表结构如下:
UniqueID(主键,自动增长)   |   CompanyName   |   CompanyCode   |   Address   |   Owner   |   Memo
共6个字段。
  1. SqlConnection sqlconnection = new SqlConnection(connectionString);
  2. SqlDataAdapter sqldataadapter = new SqlDataAdapter("select * from Table_1 where 1=2", sqlconnection);
  3. DataSet dataset = new DataSet();
  4. sqldataadapter.Fill(dataset, "Table_1");
  5. DataTable datatable = dataset.Tables[0];
  6. //生成20000条记录
  7. for (int i = 0; i < 20000; i++)
  8. {
  9.       DataRow datarow = datatable.NewRow();
  10.       datarow["CompanyName"] = "companyname"+string.Format("{0:0000}",i);
  11.       datarow["CompanyCode"] = "companycode" + string.Format("{0:0000}", i);
  12.       datarow["Address"] = "address" + string.Format("{0:0000}", i);
  13.       datarow["Owner"] = "owner" + string.Format("{0:0000}", i);
  14.       datarow["Memo"] = "memo" + string.Format("{0:0000}", i);
  15.       datatable.Rows.Add(datarow);
  16. }
复制代码
2、使用sqlcommand.executenonquery()方法插入
  1. foreach (DataRow datarow in datatable.Rows)
  2. {
  3. string sql = "INSERT INTO [Table_1]([CompanyName],[CompanyCode],[Address],[Owner],
  4. )" +
  5. "VALUES('" + datarow["CompanyName"].ToString() + "'" +
  6. ",'" + datarow["CompanyCode"].ToString() + "'" +
  7. ",'" + datarow["Address"].ToString() + "'" +
  8. ",'" + datarow["Owner"].ToString() + "'" +
  9. ",'" + datarow["Memo"].ToString() + "')";
  10.    using (SqlConnection sqlconn = new SqlConnection(connectionString))
  11.    {
  12.    sqlconn.Open();    SqlCommand sqlcommand = new SqlCommand(sql, sqlconn);
  13.    sqlcommand.ExecuteNonQuery();
  14.    sqlconn.Close();
  15.    }
  16. }
复制代码
插入20000条记录时间:00:00:29.7336000

3、使用sqldataadapter.update(dataset,tablename);
  1. SqlCommand insertcommand = new SqlCommand("INSERT INTO [Table_1]([CompanyName],[CompanyCode],[Address],[Owner],
  2. )" +
  3. "VALUES(@CompanyName, @CompanyCode,@Address,@Owner,@Memo)",new SqlConnection(connectionString));
  4. insertcommand.Parameters.Add("@CompanyName", SqlDbType.NChar, 50, "CompanyName");
  5. insertcommand.Parameters.Add("@CompanyCode", SqlDbType.NChar, 25, "CompanyCode");
  6. insertcommand.Parameters.Add("@Address", SqlDbType.NChar, 255, "Address");
  7. insertcommand.Parameters.Add("@Owner", SqlDbType.NChar, 25, "Owner");
  8. insertcommand.Parameters.Add("@Memo", SqlDbType.NChar, 255, "Memo");
  9. sqldataadapter.InsertCommand = insertcommand;
  10. sqldataadapter.Update(dataset, "Table_1");
复制代码
插入20000条记录时间:00:00:22.8938000

4、使用sqlbulkcopy.writetoserver(datatable)
  1. SqlBulkCopy sqlbulkcopy = new SqlBulkCopy(connectionString, SqlBulkCopyOptions.UseInternalTransaction);
  2. sqlbulkcopy.DestinationTableName = "Table_1";//数据库中的表名
  3. sqlbulkcopy.WriteToServer(dataset.Tables[0]);
复制代码
插入20000条记录时间:00:00:00.3276000

所以说速度是sqlbulkcopy最快,sqldataadapter.update()次之,sqlcommand.ExecuteNonQuery()最慢。

你可能感兴趣的:(DataTable数据批量写入数据库三种方法比较)