sqltransaction.commit不更新数据库

时间:2015-07-04 10:46:04

标签: sql-server vb.net transactions

我正在使用vb.net和带有Advanced Services ver 10.0的SQL Server Express版

我正在我的函数makerecharge中实现事务和回滚。以下是我的功能

代码执行良好且不显示任何错误,但数据库未更新。

请告诉我我的代码中有错误或者我必须更改数据库设置

Private Function makeRecharge(ByVal balDealer As Integer, ByVal balClient As Integer _
                              , ByVal rechQty As Integer _
                              , ByVal strBalExp As String, ByVal remark As String _
                              , ByVal browserInfo As String, ByVal clientIP As String) As Boolean
    Dim success As Boolean
    Dim con As SqlConnection

    con = New SqlConnection(ConfigurationManager.ConnectionStrings("SMSnMMSConnectionString").ConnectionString)

    Dim transaction As SqlTransaction
    Dim dbCmd As SqlCommand
    Dim strSql As String

    If con.State = ConnectionState.Closed Then con.Open()

    transaction = con.BeginTransaction

    success = False
    transaction.GetType()

    Try
        'Insert Record In RechargeRecord table
        strSql = "INSERT INTO dbo.[RechargeRecord] (RechargeMode, PartnerID,  ClientID, ClientName, RechargeDateTime, PreferredRoute, BalanceBefRecharge, RechargeQty, RechargeAmt, InvoiceNo, DealerID, BalanceExpiryDate, Remark, BrowserType,ClientIP) " + _
        " VALUES(@RechargeMode, @PartnerID,  @ClientID, @ClientName, @RechargeDateTime, @PreferredRoute, @BalanceBefRecharge, @RechargeQty, @RechargeAmt, @InvoiceNo, @DealerID, @BalanceExpiryDate, @Remark, @BrowserType,@ClientIP)"
        If con.State = ConnectionState.Closed Then con.Open()
        dbCmd = New SqlCommand(cmp.strSql, con, transaction)
        dbCmd.Parameters.Add(New SqlParameter("@RechargeMode", "Dealer Transfer"))
        dbCmd.Parameters.Add(New SqlParameter("@PartnerID", Session("ssPartnerID").ToString))
        dbCmd.Parameters.Add(New SqlParameter("@ClientID", Session("SubClientID").ToString))
        dbCmd.Parameters.Add(New SqlParameter("@ClientName", Session("Dealers_Client_Name").Replace("'", "''").ToString))
        dbCmd.Parameters.Add(New SqlParameter("@RechargeDateTime", DateTime.Now.ToString("MM/dd/yyyy hh:mm tt")))
        dbCmd.Parameters.Add(New SqlParameter("@PreferredRoute", Session("ssPreferredRoute").ToString))
        dbCmd.Parameters.Add(New SqlParameter("@BalanceBefRecharge", balClient))
        dbCmd.Parameters.Add(New SqlParameter("@RechargeQty", rechQty))
        dbCmd.Parameters.Add(New SqlParameter("@RechargeAmt", "0"))
        dbCmd.Parameters.Add(New SqlParameter("@InvoiceNo", "0"))
        dbCmd.Parameters.Add(New SqlParameter("@DealerID", Session("ssClientId").ToString))

        If strBalExp <> Nothing Then
            dbCmd.Parameters.Add(New SqlParameter("@BalanceExpiryDate", strBalExp))
        Else
            dbCmd.Parameters.Add(New SqlParameter("@BalanceExpiryDate", SqlDateTime.Null))
        End If

        dbCmd.Parameters.Add(New SqlParameter("@Remark", Left(remark, 250)))
        dbCmd.Parameters.Add(New SqlParameter("@BrowserType", browserInfo))
        dbCmd.Parameters.Add(New SqlParameter("@ClientIP", clientIP))

        dbCmd.ExecuteNonQuery()

        'Update Dealer Ballance
        strSql = "UPDATE dbo.[ClientMaster] SET BalanceSMS = BalanceSMS - @RechargeQuantity WHERE ClientID = @ClientID"

        If con.State = ConnectionState.Closed Then con.Open()

        dbCmd = New SqlCommand(cmp.strSql, con, transaction)
        dbCmd.Parameters.Add(New SqlParameter("@RechargeQuantity", rechQty))
        dbCmd.Parameters.Add(New SqlParameter("@ClientID", Session("ssClientId").ToString))

        dbCmd.ExecuteNonQuery()

        'Update Client Ballance
        strSql = "UPDATE dbo.[ClientMaster] SET BalanceSMS = BalanceSMS + @RechargeQuantity, LastRechargeDate = @LastRechargeDate, DemoAccount = @DemoAccount, BalanceExpiryDate = @BalanceExpiryDate, RechargeAlertBalance = @RechargeAlertBalance WHERE ClientID = @ClientID"

        If con.State = ConnectionState.Closed Then con.Open()

        dbCmd = New SqlCommand(cmp.strSql, con, transaction)
        dbCmd.Parameters.Add(New SqlParameter("@RechargeQuantity", rechQty))
        dbCmd.Parameters.Add(New SqlParameter("@LastRechargeDate", DateTime.Now.ToString("MM/dd/yyyy")))
        dbCmd.Parameters.Add(New SqlParameter("@DemoAccount", "0"))

        If strBalExp <> Nothing Then
            dbCmd.Parameters.Add(New SqlParameter("@BalanceExpiryDate", strBalExp))
        Else
            dbCmd.Parameters.Add(New SqlParameter("@BalanceExpiryDate", SqlDateTime.Null))
        End If
        dbCmd.Parameters.Add(New SqlParameter("@RechargeAlertBalance", CInt(txtRechargeAlertBalance.Text)))
        dbCmd.Parameters.Add(New SqlParameter("@ClientID", Session("SubClientID").ToString))

        dbCmd.ExecuteNonQuery()

        'Commit Recharge
        transaction.Commit()
        transaction.Dispose()
        success = True
    Catch ex As Exception
        'Cancel Recharage
        transaction.Rollback()
        'If ex.Number <> 2601 Then '2601
        cmp.strFailedSQL = ""
        cmp.SendEmail(ConfigurationManager.AppSettings("DBAdminEmail"), ConfigurationManager.AppSettings("DBAdminEmail"), "SMSnMMS: Error in InsertDatabase", "Error:" + vbCrLf + ex.Message + vbCrLf + "SQL:" + vbCrLf + cmp.strFailedSQL, False)
        cmp.LogException("Make Recharge - Dealer", "Error:" + vbCrLf + ex.Message + vbCrLf + "SQL:" + vbCrLf + cmp.strFailedSQL)
        'End If
    Finally
        If con.State = ConnectionState.Open Then con.Close()
    End Try

    Return success
End Function

0 个答案:

没有答案