Solved

multiple transactions

Posted on 2012-03-30
2
279 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now