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

该操作对于事务状态无效

以下是我的电话 struct :

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
        //
    }
}

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

推荐答案

在做了一些研究之后,似乎我不能用TransactionScope块打开同一数据库的两个连接.我需要修改代码,使其看起来像这样:

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
    }
}

.net相关问答推荐

如何运行大量阻塞/同步 I/O 操作

Dotnet 反射:使用 F# 中的out参数调用 MethodInfo 上的调用

为什么 .NET 中的 System.Version 定义为 Major.Minor.Build.Revision?

如何获取 Sql Server 数据库中所有模式的列表

在 .NET C# 中存储加密密钥的最佳方式

IEnumerable Count() 和 Length 的区别

Int 到字节数组

DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") 返回上午时间而不是下午时间?

非通用 TaskCompletionSource 或替代

如何在 WebBrowser 控件中注入 Javascript?

内存分配:堆栈与堆?

如何以编程方式删除 WebClient 中的 2 个连接限制

如何在 nuspec 中指定特定的依赖版本?

MailMessage,Sender 和 From 属性的区别

DataContractSerializer vs XmlSerializer:每个序列化器的优缺点

记录器包装器最佳实践

ADO.NET Entity Framework:更新向导不会添加表

序列化和反序列化 .NET 对象的最快方法

序列化一个可为空的 int

判断数据表中是否包含空值的最佳方法