user1051505
user1051505

Reputation: 962

update oracle statement doesn't work

I am trying to update a record with VB.Net. This is the code. It doesn't give any error. But every time I run this code iI expect that the variable newbal will increment. It doesn't happen. That means the update statement doesn't work. Any workaround?

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()

Upvotes: 0

Views: 1598

Answers (2)

LeftyX
LeftyX

Reputation: 35597

You should use this structure to manage transactions with Oracle (see 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

Upvotes: 3

NoNaMe
NoNaMe

Reputation: 6242

oracle asks for explicit commit, possible that is missing

Upvotes: 2

Related Questions