0

以下に、最初の SProc から autoID を取得し、それを別の SProc にパラメーターとして渡す関数の一部を示します。ここではトランザクション ロールバックを使用しています。たとえば、最初の SProc ですべてが正常に実行されたが、2 番目の SProc で問題が発生した場合、最初の SProc 操作も 2 番目の SProc と一緒にロールバックされますか? ずっと、1 つの SProc トランザクションを処理するだけで済みますが、これは私にとっては少し異なります。ありがとう。

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
4

1 に答える 1

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
于 2012-05-04T02:43:51.327 に答える