c# - 该操作对交易状态无效

标签 c# transactions transactionscope

我有一个 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/

相关文章:

mysql - 是否有理由将数据库事务用于只读 sql 语句?

java - 为什么 EJB 无状态 session bean 不能实现 SessionSynchronization?

grails - 服务 chalice 的多种方法

exception - 这些 MS DTC 异常是什么意思?

c# - WPF:DataGrid 在值更改时排序

c# - 如何更改默认的 Visual Studio C# 新类文件模板?

c# - 客户端通常在几十分钟后与服务器断开连接

c# - NHibernate 和运算符重载

mysql - 确保 MySQL 事务在 COMMIT 时失败

asp.net-mvc - 如何在 .net MVC 中创建安全的每个 Web 请求事务?