更新oracle语句不起作用

时间:2012-08-30 05:13:04

标签: sql vb.net oracle

我正在尝试使用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()

2 个答案:

答案 0 :(得分:3)

您应该使用此结构来管理与Oracle的交易(请参阅MSDN docs):

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

答案 1 :(得分:2)

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