2008-10-10 73 views
41

我收到以下錯誤,當我嘗試調用包含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 
     // 
    } 
} 

與我創建另一個連接問題離子到交易中的同一個數據庫?

回答

41

在做了一些研究後,似乎我不能用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 
    } 
} 
+0

我偶然發現了同樣的情況。我必須在同一個事務範圍內引用兩個不同的數據庫。謝謝你的提示。 – rageit 2012-04-09 12:52:32

1

我遇到了這個錯誤,當我的事務嵌套在另一個。存儲過程是否有可能聲明自己的事務,或者調用函數聲明瞭它?

+0

我沒有任何任何T-SQL交易代碼我的存儲過程。理論上,事務應該由MyAddUpdateMethod() – 2008-10-10 22:04:39

+0

+1控制,因爲當我的存儲過程中的事務語言錯誤時(即事務計數被搞砸),我收到了這個錯誤。 – codeMonkey 2017-03-28 16:48:05

7

我也碰到過同樣的問題,我改變了事務超時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(); 
    ... 

} 
5

當我遇到這個異常時,出現了一個InnerException「Transaction Timeout」。由於這是在調試會話期間,當我在TransactionScope中暫停我的代碼一段時間時,我選擇忽略此問題。

當超時這個特定的異常出現在部署的代碼,我認爲在你config文件的以下部分將幫助你:

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