“该操作对于交易状态无效"错误和事务范围 [英] "The operation is not valid for the state of the transaction" error and transaction scope

查看:18
本文介绍了“该操作对于交易状态无效"错误和事务范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试调用包含 SELECT 语句的存储过程时出现以下错误:

I am getting the following error when I try to call a stored procedure that contains a SELECT Statement:

该操作对交易状态无效

这是我的通话结构:

public void MyAddUpdateMethod()
{

    using (TransactionScope Scope = new TransactionScope(TransactionScopeOption.RequiresNew))
    {
        using(SQLServer Sql = new SQLServer(this.m_connstring))
        {
            //do my first add update statement

            //do my call to the select statement sp
            bool DoesRecordExist = this.SelectStatementCall(id)
        }
    }
}

public bool SelectStatementCall(System.Guid id)
{
    using(SQLServer Sql = new SQLServer(this.m_connstring)) //breaks on this line
    {
        //create parameters
        //
    }
}

我在事务中创建到同一个数据库的另一个连接有问题吗?

Is the problem with me creating another connection to the same database within the transaction?

推荐答案

经过一些研究,我似乎无法使用 TransactionScope 块打开到同一个数据库的两个连接.我需要修改我的代码看起来像这样:

After doing some research, it seems I cannot have two connections opened to the same database with the TransactionScope block. I needed to modify my code to look like this:

public void MyAddUpdateMethod()
{
    using (TransactionScope Scope = new TransactionScope(TransactionScopeOption.RequiresNew))
    {
        using(SQLServer Sql = new SQLServer(this.m_connstring))
        {
            //do my first add update statement            
        }

        //removed the method call from the first sql server using statement
        bool DoesRecordExist = this.SelectStatementCall(id)
    }
}

public bool SelectStatementCall(System.Guid id)
{
    using(SQLServer Sql = new SQLServer(this.m_connstring))
    {
        //create parameters
    }
}

这篇关于“该操作对于交易状态无效"错误和事务范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆