我有一个 TransactionScope()
block 。它总是卡在插入语句中。它作为阻塞任务出现在事件监视器中,因此它阻塞了 SQL 服务器,超时后,我收到此错误:
The operation is not valid for the state of the transaction.
出了什么问题?
const TransactionScopeOption opt = new TransactionScopeOption();
TimeSpan span = new TimeSpan(0, 0, 1, 30);
try
{
using (TransactionScope scope01 = new TransactionScope(opt, span))
{
using (var sqlcon = new SqlConnection(sSqlCon))
{
//select,insert , update statements
}
}
}
catch (Exception ex)
{
}
最佳答案
这可能意味着它中止了。您是否称交易在交易范围内完成?
try
{
using (TransactionScope scope01 = new TransactionScope(opt, span))
{
using (var sqlcon = new SqlConnection(sSqlCon))
{
//select,insert , update statements
}
scope01.Complete();
}
}
如果没有调用 Complete,它会自动回滚。
关于c# - 该操作对交易状态无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20051527/