一个事务处理的问题,帮忙解答一下,谢谢
public string UpdateOrderStatus(DataTable dt)
{
SqlConnection myconn = GetConn();
SqlCommand Cmd = new SqlCommand();
Cmd.Connection = myconn;
Cmd.CommandText = "select rowid into #tmpdata from deliverydata where 2=1";
Cmd.ExecuteNonQuery();
SqlTransaction sqlbulkTransaction = myconn.BeginTransaction();
SqlBulkCopy copy = new SqlBulkCopy(myconn, SqlBulkCopyOptions.CheckConstraints, sqlbulkTransaction);
copy.DestinationTableName = "#tmpdata";
foreach (DataColumn dc in dt.Columns)
{
copy.ColumnMappings.Add(dc.ColumnName, dc.ColumnName);
}
try
{
copy.WriteToServer(dt);
sqlbulkTransaction.Commit();
Cmd.CommandText = "insert into tracedata(rowid,inserttime,status) (select rowid,'" + DateTime.Now.ToString() + "','出仓扫描' from #tmpdata)";
Cmd.ExecuteNonQuery();
}
catch (Exception ex) {
sqlbulkTransaction.Rollback();
return(ex.ToString());
}
finally {
copy.Close();
myconn.Close();
}
return "完成";
}
我要把事务处理跟上面红色这段代码关联,请问如何修改?因为上述的事务处理只应用于SqlBulkCopy,但是没有应用于Cmd.CommandText = "insert into tracedata。。。这段代码,请问应该如何修改?
------解决方案--------------------
Cmd.Transaction = sqlbulkTransaction;
Cmd.CommandText = "insert into tracedata(rowid,inserttime,status) (select rowid,'" + DateTime.Now.ToString() + "','出仓扫描' from #tmpdata)";
Cmd.ExecuteNonQuery();
------解决方案--------------------在红色语句前加入
Cmd.Transaction = sqlbulkTransaction;
将sqlbulkTransaction.Commit();放到红色后面