2012-05-04 18 views
0

私は最初のSProcからautoIDを取得し、それを別のSProcにパラメータとして渡す、私の一部の機能を持っています。ここでは、トランザクションのロールバックを使用していますが、最初のSProcがすべて正常に実行されたが、2番目のSProcで問題が発生した場合、最初のSProc操作は2番目のSProcと一緒にロールバックされます。私はすべて、1つのSProcトランザクションに対処する必要がありますが、これは私のために異なっています。ありがとう。asp.netとSQL Server +トランザクションのロールバック

Public Shared Function VoucherRedemption(ByVal dbTrans As DbTransaction _ 
             , ByVal dbConnection As DbConnection _ 
             , ByVal receiptNo As String _ 
             , ByVal voucherNo As String _ 
             , ByVal customerCode As String) As Boolean 
Dim dbCommand As DbCommand = Nothing 

Try 
      If DbConnection.State <> ConnectionState.Open Then 
       DbConnection.Open() 
      End If 

      dbCommand = GetStoredProcedureCommand("Mem_Redeem") 
      dbCommand.Connection = DbConnection 
      dbCommand.Transaction = dbTrans 

      AddInParameter(dbCommand, "@ReceiptNo", DbType.String, 50, DBNull.Value) 
      If Not String.IsNullOrEmpty(receiptNo) Then 
       dbCommand.Parameters("@ReceiptNo").Value = receiptNo 
      End If 

      AddOutParameter(dbCommand, "@OutAutoIDs", DbType.String, 4000, DBNull.Value) 

      ExecuteNonQuery(dbCommand) 

      Dim outAutoIDs As String = CType(dbCommand.Parameters("@OutAutoIDs").Value, String) 

      dbCommand = GetStoredProcedureCommand("Mem_Redeem_Log_Add") 
      dbCommand.Connection = DbConnection 
      dbCommand.Transaction = dbTrans 

      AddInParameter(dbCommand, "@VoucherNo", DbType.String, 50, DBNull.Value) 
      dbCommand.Parameters("@VoucherNo").Value = voucherNo 

      AddInParameter(dbCommand, "@RedeemTransactAutoID", DbType.String, 4000, DBNull.Value) 
      dbCommand.Parameters("@RedeemTransactAutoID").Value = outAutoIDs 

    ExecuteNonQuery(dbCommand) 

     Catch ex As Exception 
      Throw New DALException(ex, dbCommand, customerCode, "VoucherRedemption") 
     Finally 
      If Not dbCommand Is Nothing Then 
       dbCommand.Dispose() 
      End If 
      If Not dbTrans Is Nothing Then 
       dbTrans.Dispose() 
      End If 
     End Try 

答えて

1

2つのストアドプロシージャを使用しているようです。だからではなくDbTransaction の 使用TransactionScope、ここかもしれないあなたのコードについて

Public Shared Function VoucherRedemption(ByVal dbTrans As DbTransaction _ 
             , ByVal dbConnection As DbConnection _ 
             , ByVal receiptNo As String _ 
             , ByVal voucherNo As String _ 
             , ByVal customerCode As String) As Boolean 
Dim dbCommand As DbCommand = Nothing 
Using scope As New TransactionScope() 
Try 
      If DbConnection.State <> ConnectionState.Open Then 
       DbConnection.Open() 
      End If 

      dbCommand = GetStoredProcedureCommand("Mem_Redeem") 
      dbCommand.Connection = DbConnection 

      AddInParameter(dbCommand, "@ReceiptNo", DbType.String, 50, DBNull.Value) 
      If Not String.IsNullOrEmpty(receiptNo) Then 
       dbCommand.Parameters("@ReceiptNo").Value = receiptNo 
      End If 

      AddOutParameter(dbCommand, "@OutAutoIDs", DbType.String, 4000, DBNull.Value) 

      ExecuteNonQuery(dbCommand) 

      Dim outAutoIDs As String = CType(dbCommand.Parameters("@OutAutoIDs").Value, String) 

      dbCommand = GetStoredProcedureCommand("Mem_Redeem_Log_Add") 
      dbCommand.Connection = DbConnection 

      AddInParameter(dbCommand, "@VoucherNo", DbType.String, 50, DBNull.Value) 
      dbCommand.Parameters("@VoucherNo").Value = voucherNo 

      AddInParameter(dbCommand, "@RedeemTransactAutoID", DbType.String, 4000, DBNull.Value) 
      dbCommand.Parameters("@RedeemTransactAutoID").Value = outAutoIDs 

    ExecuteNonQuery(dbCommand) 

     Catch ex As Exception 
      Throw New DALException(ex, dbCommand, customerCode, "VoucherRedemption") 
     Finally 
      If Not dbCommand Is Nothing Then 
       dbCommand.Dispose() 
      End If 
     End Try 
    scope.Complete() 
End Using 
関連する問題