2012-01-12 33 views
6

次のコードを実行しようとすると、このエラーメッセージが表示されます。ExecuteNonQueryは、コマンドにトランザクションが必要です

ExecuteNonQuery requires the command to have a transaction when the connection assigned to the command is in a pending local transaction 

問題のある人は誰でも助言できますか?私は、問題の根源は私がストアドプロシージャを実行しようとする部分だと思います。 far.youがSqlTransactionに関連するすべてのものを削除してから、TransactionScope

+0

マイナーポイント - あなたは 'finally'にコミットしようとするべきではない - 、代わりに、' try'にする必要があり、あなたの操作の後 –

+0

は少し古いかもしれませんが、あなたがいることを確認してくださいすることができます私の答えはあなたの問題を解決しましたか? – Afshin

答えて

2

を実行したとき

ストアドプロシージャさは、独自のトランザクションを作成しますトランザクションを使用する場合は、どこでも使用する必要があります。

cmd.Transaction = transaction; 
+1

TransactionScopeには十分注意してください。それがうまくいくように見えます。しかし、私はそれが非ローカルSQLサーバーをターゲットとするときに非常に脆弱であることを発見しました。あなたはセットアップが "ちょうどいい"ものが必要です。 –

11

でコードをラップすることができるように普及していない接続文字列のトランザクションを使用して

using (SqlConnection conn = new SqlConnection(connStr)) 
      { 
       conn.Open(); 

       SqlCommand command = conn.CreateCommand(); 
       SqlTransaction transaction; 

       // Start a local transaction. 
       transaction = conn.BeginTransaction("createOrder"); 

       // Must assign both transaction object and connection 
       // to Command object for a pending local transaction 
       command.Connection = conn; 
       command.Transaction = transaction; 

       try 
       { 
        command.CommandText = "INSERT INTO rand_resupply_order (study_id, centre_id, date_created, created_by) " + 
         "VALUES (@study_id, @centre_id, @date_created, @created_by) SET @order_id = SCOPE_IDENTITY()"; 

        command.Parameters.Add("@study_id", SqlDbType.Int).Value = study_id; 
        command.Parameters.Add("@centre_id", SqlDbType.Int).Value = centre_id; 
        command.Parameters.Add("@date_created", SqlDbType.DateTime).Value = DateTime.Now; 
        command.Parameters.Add("@created_by", SqlDbType.VarChar).Value = username; 

        SqlParameter order_id = new SqlParameter("@order_id", SqlDbType.Int); 
        //study_name.Value = 
        order_id.Direction = ParameterDirection.Output; 
        command.Parameters.Add(order_id); 

        command.ExecuteNonQuery(); 
        command.Parameters.Clear(); 

        //loop resupply list 
        for (int i = 0; i < resupplyList.Count(); i++) 
        { 
         try 
         { 
          SqlCommand cmd = new SqlCommand("CreateOrder", conn); 
          cmd.CommandType = CommandType.StoredProcedure; 

          cmd.Parameters.Add("@study_id", SqlDbType.Int).Value = study_id; 
          cmd.Parameters.Add("@centre_id", SqlDbType.Int).Value = centre_id; 
          cmd.Parameters.Add("@created_by", SqlDbType.VarChar).Value = username; 
          cmd.Parameters.Add("@quantity", SqlDbType.VarChar).Value = resupplyList[i].Quantity; 
          cmd.Parameters.Add("@centre_id", SqlDbType.Int).Value = centre_id; 
          cmd.Parameters.Add("@depot_id", SqlDbType.VarChar).Value = depot_id; 
          cmd.Parameters.Add("@treatment_code", SqlDbType.Int).Value = centre_id; 
          cmd.Parameters.Add("@order_id", SqlDbType.Int).Value = (int)order_id.Value; 
          cmd.ExecuteNonQuery(); 
         } 
         catch (SqlException ex) 
         { 
          transaction.Rollback(); 
          ExceptionUtility.LogException(ex, "error"); 
          throw ex; 
         } 
         catch (Exception ex) 
         { 
          transaction.Rollback(); 
          ExceptionUtility.LogException(ex, "error"); 
          throw ex; 
         } 
         finally 
         { 
          conn.Close(); 
          conn.Dispose(); 
         } 

        } 

        return (int)order_id.Value; 

       } 
       catch (Exception ex) 
       { 
        transaction.Rollback(); 
        ExceptionUtility.LogException(ex, "error"); 
        throw ex; 
       } 
       finally 
       { 
        // Attempt to commit the transaction. 
        transaction.Commit(); 

        conn.Close(); 
        conn.Dispose(); 
        command.Dispose(); 
       } 
+0

ストアドプロシージャ自体に独自のトランザクションが含まれている場合は重要ですか? – pothios

+0

実際にはそうです.netの観点からします。 – Afshin

+0

質問はどのように私は独自のトランザクションでストアドプロシージャを呼び出すトランザクションの実行を達成することができますか:( – pothios

関連する問題