Visual Basic Transactions

Posted on 2007-03-29
Last Modified: 2012-08-13
Visual basic transactions - I have stored procedures called from visual basic. How do I create transactions in visual basic?

For example, I'd like to do:

start transaction
vb code
stored procedure call
vb code
stored procedure call
if error, rollback
Question by:mhunts
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 18

Expert Comment

by:Ajay Sharma
ID: 18821915

Author Comment

ID: 18821959
Not quite. The example only calls one stored procedure. Look at my question again.
LVL 18

Assisted Solution

DarrenD earned 100 total points
ID: 18822092

I have some code in C# to do this if you want to take it and convert it into VB.NET. I haven't out up any code as yet as I'm not sure if C# is any use to you.

Put simply you have to do the following:

This is performed in one method to setup the connection and transaction

// Create the connection.
// open the connection.
// Set the beginning of the transaction.

This is performed in another method so that you can call as many SP's as you want.
// Tell the command object the name of the SP to run.
// Tell the command object that it is part of a transaction.
// Tell the command object that it is going to run a SP.
// Execute the Query

Each time you call the second method above you must clear any parameters that you added for the call to the previous SP.

Then there is another method which I use to either commit the transaction or rollback.

I just have a simgle class which performs all of these operation for me. It's just a DAL.


Accepted Solution

dctuck earned 400 total points
ID: 18822589
       Dim myTrans As System.Data.SqlClient.SqlTransaction
        Dim myConn As New SqlClient.SqlConnection("connection string")
            myTrans = myConn.BeginTransaction
            Dim myComm As New SqlClient.SqlCommand("Command text",myConn)
            myComm.Transaction = myTrans
           ' Perform your actions....
           ' Repeat the previous two steps for any additional commands
        End Try

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

636 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