79

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

トランザクション内で同じデータベースへの別の接続を作成することに問題がありますか?

4

9 に答える 9

66

いくつかの調査を行った後、TransactionScope ブロックを使用して同じデータベースに対して 2 つの接続を開くことはできないようです。コードを次のように変更する必要がありました。

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
    }
}
于 2008-10-15T01:07:19.267 に答える
13

私も同じ問題に遭遇しました.トランザクションタイムアウトを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();
    ...

}
于 2010-11-30T11:36:21.353 に答える
5

トランザクションが別のトランザクション内にネストされているときに、このエラーが発生しました。ストアドプロシージャが独自のトランザクションを宣言すること、または呼び出し元の関数がトランザクションを宣言することは可能ですか?

于 2008-10-10T21:55:00.607 に答える
0

同時に 2 つのトランザクションを開くことはできません。私がしていることは、2 番目のトランザクションで使用される結果を返す前にtransaction.Complete()を指定することです;)

于 2021-08-10T08:08:44.687 に答える