Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Greetings,

I'm implementing an application using vb.net 2005 with oracle 10g and I came across a functionality that should Select/Insert/Update/Delete data from three different tables in oracle.

So I`m wondering if there is a way to prepare all the 3 commands (SQL Statements) separately and then execute them in one shot to the oracle, so in this way I can guarantee that all of them are successfully executed or all are failed to execute, and also I could gain more performance which is critical in my case.

Thus I`m looking for a syntax in vb.net that helps me execute more than one OracleCommand in one shot to the oracle.

So please advise...
Posted

What you are actually looking to do is wrap them in a transaction. Fortunately for you, the OracleConnection class provides a BeginTransaction method (this returns an OracleTransaction). Simply call all your commands inside this connection, then call Commit() to save the changes if they execute successfully, or Rollback if one of the statement fails. A sample would look like this:
VB
Public Sub RunCommands(connection As OracleConnectioncommands As List(Of OracleCommand))
    Dim transaction As OracleTransaction
    connection.Open()
    ' Start a local transaction
    transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted)
    Try
      For Each command As OracleCommand In commands
        command.Connection = connection
        command.ExecuteNonQuery
      Next
      transaction.Commit()
    Catch e As Exception
      transaction.Rollback()
      Throw
    End Try
End Sub
 
Share this answer
 
v2
Comments
dgthecodeproject 24-Apr-12 7:35am    
Thank you Pete..

I wonder where the execution method of the command is exist in your sample code...I tried your way but nothing executed to the database....please advise.
Pete O'Hanlon 24-Apr-12 7:47am    
Oops, sorry. It goes after you assign the connection to command.Connection. Sorry - I just typed this in the editor, and I was busy concentrating on converting C# code to VB.NET (I'm a C# coder not a VB.NET one).
uspatel 24-Apr-12 8:00am    
Yes,This is a good way.......
5+
dgthecodeproject 24-Apr-12 8:56am    
Thanks very much Pete for your interest..Now it is running successfully...But do you think this way has better performance than executing each command alone and without using the transaction object??...
Pete O'Hanlon 24-Apr-12 9:21am    
You can't really compare the two. As you have stated that you have to guarantee that the database is updated only if all the statements complete, then the transaction is your only option. It will performly slightly worse than issuing the commands outside a transaction because you have the overhead of creating the transaction; but executing the commands outside a transaction will leave your database in an inconsistent state if one of the statements fails.
It's not possible to combine queries within one OleDbCommand.
If possible, make a stored procedure.
 
Share this answer
 
Comments
Pete O'Hanlon 24-Apr-12 5:43am    
Of course it is possible - and he's using OracleCommand. To use multiple commands in one command, he would need to separate them by semi colons, and wrap them in a begin...end;
dgthecodeproject 24-Apr-12 8:54am    
How is that Pete...Can you show me a sample here if possible...
Pete O'Hanlon 24-Apr-12 9:26am    
From memory (and typed in this textbox), it would look like this:
Dim sql as New StringBuilder
sql.Append("begin ")
sql.Append("INSERT .....; ")
sql.Append("INSERT .....; ")
sql.APPEND("INSERT .....; ")
sql.Append("end;")
dgthecodeproject 24-Apr-12 9:45am    
Thx man..Its a good idea but not in my case...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900