发布网友 发布时间:2022-04-07 22:52
共2个回答
懂视网 时间:2022-04-08 03:13
要将datatable 批量更新到数据表其实是两步:
(1)将datatable快速存入一个表中;
(2)根据一个数据表更新另外一个数据表;
下面细说:
(1)将datatable快速存入一个表中;
1) insert循环插入;
2) sqldataadapter.update(dataset,tablename);
3) sqlbulkcopy.WriteToServer(datatable);
1、生成测试的datatable表,表结构如下:
UniqueID(主键,自动增长) | CompanyName | CompanyCode | Address | Owner | Memo
共6个字段。
SqlConnection sqlconnection = new SqlConnection(connectionString);
SqlDataAdapter sqldataadapter = new SqlDataAdapter("select * from Table_1 where 1=2", sqlconnection);
DataSet dataset = new DataSet();
sqldataadapter.Fill(dataset, "Table_1");
DataTable datatable = dataset.Tables[0];
//生成20000条记录
for (int i = 0; i < 20000; i++)
{
DataRow datarow = datatable.NewRow();
datarow["CompanyName"] = "companyname"+string.Format("{0:0000}",i);
datarow["CompanyCode"] = "companycode" + string.Format("{0:0000}", i);
datarow["Address"] = "address" + string.Format("{0:0000}", i);
datarow["Owner"] = "owner" + string.Format("{0:0000}", i);
datarow["Memo"] = "memo" + string.Format("{0:0000}", i);
datatable.Rows.Add(datarow);
}
2、使用sqlcommand.executenonquery()方法插入
foreach (DataRow datarow in datatable.Rows)
{
string sql = "INSERT INTO [Table_1]([CompanyName],[CompanyCode],[Address],[Owner],
)" +
"VALUES(‘" + datarow["CompanyName"].ToString() + "‘" +
",‘" + datarow["CompanyCode"].ToString() + "‘" +
",‘" + datarow["Address"].ToString() + "‘" +
",‘" + datarow["Owner"].ToString() + "‘" +
",‘" + datarow["Memo"].ToString() + "‘)";
using (SqlConnection sqlconn = new SqlConnection(connectionString))
{
sqlconn.Open(); SqlCommand sqlcommand = new SqlCommand(sql, sqlconn);
sqlcommand.ExecuteNonQuery();
sqlconn.Close();
}
}
插入20000条记录时间:00:00:29.7336000
3、使用sqldataadapter.update(dataset,tablename);
SqlCommand insertcommand = new SqlCommand("INSERT INTO [Table_1]([CompanyName],[CompanyCode],[Address],[Owner],
)" +
"VALUES(@CompanyName, @CompanyCode,@Address,@Owner,@Memo)",new SqlConnection(connectionString));
insertcommand.Parameters.Add("@CompanyName", SqlDbType.NChar, 50, "CompanyName");
insertcommand.Parameters.Add("@CompanyCode", SqlDbType.NChar, 25, "CompanyCode");
insertcommand.Parameters.Add("@Address", SqlDbType.NChar, 255, "Address");
insertcommand.Parameters.Add("@Owner", SqlDbType.NChar, 25, "Owner");
insertcommand.Parameters.Add("@Memo", SqlDbType.NChar, 255, "Memo");
sqldataadapter.InsertCommand = insertcommand;
sqldataadapter.Update(dataset, "Table_1");
插入20000条记录时间:00:00:22.8938000
使用sqlbulkcopy.writetoserver(datatable)
SqlBulkCopy sqlbulkcopy = new SqlBulkCopy(connectionString, SqlBulkCopyOptions.UseInternalTransaction);
sqlbulkcopy.DestinationTableName = "Table_1";//数据库中的表名
sqlbulkcopy.WriteToServer(dataset.Tables[0]);
插入20000条记录时间:00:00:00.3276000
所以说速度是sqlbulkcopy最快,sqldataadapter.update()次之,sqlcommand.ExecuteNonQuery()最慢。
(2)根据一个数据表更新另外一个数据表;
UPDATE A SET A.a=B.a FROM B WHERE A.b = B.b
代码的意思是根据数据表A中的b列与B中的b列之间的比较来更新 A中的a列
^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^
欢迎来我的淘宝店【老李飞充】,话费充值优惠靠谱,http://chinatrust.taobao.com
^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^
SQL快速操作技巧2_datatable批量更新到数据表
标签:
热心网友 时间:2022-04-08 00:21
SqlDataAdapter adapter = new SqlDataAdapter(strSql, strConn);