0

我正在尝试使用 VB.Net 更新记录。这是代码。它没有给出任何错误。但是每次我运行这段代码时,我都希望变量newbal会增加。它不会发生。这意味着更新语句不起作用。任何解决方法?

Dim cmd5 As New OracleCommand
cmd5.Connection = conn
cmd5.CommandText = "SELECT * FROM d009022 WHERE prdacctid = :custid AND lbrcode = :lbrcode"
cmd5.Prepare()
cmd5.Parameters.Add(":custid", customernumber)
cmd5.Parameters.Add(":lbrcode", lbr)
Try
     Dim drs As OracleDataReader = cmd5.ExecuteReader()
     drs.Read()
     Dim oldbal As Decimal = drs.Item("SHDCLRBALFCY")
     Dim newbal As Integer = CInt(oldbal) + CInt(amount)

     Dim cmd6 As New OracleCommand
     cmd6.Connection = conn
     cmd6.CommandText = "UPDATE d009022 SET shdclrbalfcy = :newbal WHERE prdacctid = :custnum AND lbrcode = :lbr"
     cmd6.Prepare()
     cmd6.BindByName = True
     cmd6.Parameters.Add(":newbal", newbal)
     cmd6.Parameters.Add(":custnum", customernumber)
     cmd6.Parameters.Add(":lbr", lbr)
     cmd6.ExecuteNonQuery()
4

2 回答 2

3

您应该使用此结构来管理与 Oracle 的事务(请参阅MSDN 文档):

Public Sub RunOracleTransaction(ByVal connectionString As String)
    Using connection As New OracleConnection(connectionString)
        connection.Open()

        Dim command As OracleCommand = connection.CreateCommand()
        Dim transaction As OracleTransaction

        ' Start a local transaction
        transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted)
        ' Assign transaction object for a pending local transaction
        command.Transaction = transaction

        Try
            command.CommandText = _
                "INSERT INTO Dept (DeptNo, Dname, Loc) values (50, 'TECHNOLOGY', 'DENVER')"
            command.ExecuteNonQuery()
            command.CommandText = _
                "INSERT INTO Dept (DeptNo, Dname, Loc) values (60, 'ENGINEERING', 'KANSAS CITY')"
            command.ExecuteNonQuery()
            transaction.Commit()
            Console.WriteLine("Both records are written to database.")
        Catch e As Exception
            transaction.Rollback()
            Console.WriteLine(e.ToString())
            Console.WriteLine("Neither record was written to database.")
        End Try
    End Using
End Sub
于 2012-08-30T09:12:19.597 回答
2

oracle 要求显式提交,可能缺少

于 2012-08-30T05:16:00.617 回答