Click here to Skip to main content
15,903,030 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is the first time I've tried using a transaction... just wondering if I have done it "right"? It seems to work ok, but that doesn't mean anything.

Just trying to update an Access database from a datatable.

VB
objConn.openConnection()
        Dim myTrans As OleDbTransaction = objConn.con.BeginTransaction()
        Dim cmd As New OleDbCommand
        cmd.Connection = objConn.con
        cmd.Transaction = myTrans
        Dim tRow As DataRow
        Dim sql As String

        Try
            For Each tRow In table.Rows
                sql = String.Format("UPDATE Pricing SET unitPrice = {0}, salesPrice = {1}, ProfitMargin = {2} WHERE ItemID = {3}", tRow(1), tRow(2), tRow(3), tRow(0))
                cmd.CommandText = sql
                cmd.ExecuteNonQuery()
            Next
            myTrans.Commit()
            MessageBox.Show("Update Complete", "Vending", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MessageBox.Show(ex.ToString & vbCrLf & "Un-doing any work that has been done!", "Vending", MessageBoxButtons.OK, MessageBoxIcon.Error)
            myTrans.Rollback()
        Finally
            objConn.closeConnection()
        End Try
Posted

1 solution

This is implemented properly. If you want to test transaction, have atleast 2 tables where you intend to insert data and invoke an exception after data in inserted in 1 table. That would rollback the data and you don't get partial data inserted in tables. This is what you expect when you implement transaction.
 
Share this answer
 
Comments
MacRaider4 28-Sep-11 12:21pm    
I've made a bunch of changes since, but have left the transaction part... thanks for the conformation though!

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