这是我的代码我已经为命令和SqlDataAdapter
添加了事务。但是我仍然得到这个错误“ExecuteNonQuery要求命令在分配给命令的连接处于挂起的本地事务中时具有事务。"我尝试用多种方法将事务添加到SqlCommand
,但仍然得到相同的错误。
using (SqlConnection connection = Database.GetConnection(connString))
{
SqlTransaction transaction = connection.BeginTransaction();
try
{
var batchCommand = new SqlCommand("spname", connection, transaction);
batchCommand.Transaction = transaction;
batchCommand.Parameters.Add("@param1", SqlDbType.Int, 4, dt.Columns[0].ColumnName);
batchCommand.CommandType = CommandType.StoredProcedure;
batchCommand.UpdatedRowSource = UpdateRowSource.None;
SqlDataAdapter adpt = new SqlDataAdapter(batchCommand.CommandText, connection);
adpt.UpdateCommand = batchCommand;
adpt.InsertCommand = batchCommand;
adpt.InsertCommand.Transaction = transaction;
adpt.UpdateCommand.Transaction = transaction;
adpt.UpdateBatchSize = 100;
int recordsImpacted = adpt.Update(dt);
transaction.Commit();
}
catch (Exception)
{
transaction.Rollback();
throw;
}
}
1条答案
按热度按时间goqiplq21#
你可以尝试同样的例子: