“该操作对于事务状态”错误和事务范围无效

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

该操作对交易状态无效

这是我的电话结构:

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

我在交易中创build另一个连接到同一个数据库的问题?

做了一些研究后,似乎我不能有两个连接与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 } } 

我也遇到了同样的问题,我改变了交易超时到15分钟,它的工作原理。 我希望这有帮助。

 TransactionOptions options = new TransactionOptions(); options.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted; options.Timeout = new TimeSpan(0, 15, 0); using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required,options)) { sp1(); sp2(); ... } 

当我遇到这个exception时,有一个InnerException“Transaction Timeout”。 由于这是在debugging会话期间,当我在TransactionScope中暂停我的代码一段时间时,我select忽略这个问题。

当这个特定的超时出现在部署的代码中时,我认为你的.config文件中的以下部分将帮助你:

 <system.transactions> <machineSettings maxTimeout="00:05:00" /> </system.transactions> 

我遇到了这个错误,当我的事务嵌套在另一个。 存储过程是否有可能声明自己的事务,或者调用函数声明了它?