当我尝试调用包含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 // } }
我在问题中创建了另一个与同一数据库的连接的问题吗?
在做了一些研究之后,我似乎无法通过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(); ... }
当我遇到此异常时,有一个InnerException"Transaction Timeout".由于这是在调试会话期间,当我在TransactionScope中暂停我的代码一段时间后,我选择忽略此问题.
当部署的代码中出现超时的特定异常时,我认为.config文件中的以下部分将帮助您: