Solved

multiple transactions

Posted on 2012-03-30
2
281 Views
Last Modified: 2012-03-30
Hi

I want to use transactions in a vb.net application. This is the working code I have so far:

       Dim sqlCommand As String = "SELECT * FROM tblMyTable"

        Dim da1 As New SqlClient.SqlDataAdapter

        da1 = New SqlClient.SqlDataAdapter(sqlCommand, objConnection)

        Dim cmdClientsBuilder As New SqlClient.SqlCommandBuilder
        cmdClientsBuilder = New SqlClient.SqlCommandBuilder(da1)
        da1.InsertCommand = cmdClientsBuilder.GetInsertCommand()
        da1.DeleteCommand = cmdClientsBuilder.GetDeleteCommand()
        da1.UpdateCommand = cmdClientsBuilder.GetUpdateCommand()

        Dim txn As SqlClient.SqlTransaction = objConnection.BeginTransaction

        da1.UpdateCommand.Transaction = txn
        da1.DeleteCommand.Transaction = txn
        da1.InsertCommand.Transaction = txn

        Try
            da1.Update(Me.dt1)
            txn.Commit()
            Console.WriteLine("Insert succeeded")
        Catch ex As Exception
            txn.Rollback()
            MessageBox.Show(ex.Message)
            Console.WriteLine("Insert Failed")
        Finally
            'Optional. A Finally block is always executed when execution leaves any part of the Try statement.
        End Try

HOWEVER, I am puzzled how to use this if I had another update which I wanted to perform. For example, suppose I wanted to update another table as well e.g. dt2

        Try
            da1.Update(Me.dt1)
            da2.Update(Me.dt2)
            txn.Commit()
            Console.WriteLine("Insert succeeded")

To do this, would I need to have two transaction objects?

Could somebody point me in the right direction, with example code, on how to do this?

Thanks in advance

John
0
Comment
Question by:IssacJones
2 Comments
 
LVL 8

Accepted Solution

by:
gpizzuto earned 300 total points
ID: 37786487
You have to set the transaction property also to da2.

da2.UpdateCommand.Transaction = txn
da2.DeleteCommand.Transaction = txn
da2.InsertCommand.Transaction = txn

Beware: the transaction will rollback BOTH the updates if one of them fails.
0
 

Author Closing Comment

by:IssacJones
ID: 37786565
Many thanks!!!
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.

822 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question