gpt4 book ai didi

c# - "The operation is not valid for the state of the transaction"错误和交易范围

转载 作者:IT王子 更新时间:2023-10-29 03:47:32 29 4
gpt4 key购买 nike

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

The operation is not valid for the state of the transaction

这是我调用的结构:

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 block 打开两个连接到同一个数据库。我需要将代码修改为如下所示:

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

关于c# - "The operation is not valid for the state of the transaction"错误和交易范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/193154/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com