Solved

How can I check if a transaction has been started from a OleDbConnection in VB.NET?

Posted on 2012-03-20
3
863 Views
Last Modified: 2012-06-27
I'm writing a VB.NET program i´n which I use a oledbconnection to connect to an Access database. Sometimes a database action consists of several inserts and updates, and I use a transaction for this purpose so that I can roll back the entire action if a part of it fails.
However as the code logic is currently set up it would be convenient to be able to check if a transaction has been started, how do I do that? Do I simply check if the transaction is Nothing or does the oledbconnection class provide a better way?
0
Comment
Question by:andreas_rafn
[X]
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
3 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 450 total points
ID: 37741385
Check the transaction for nothing and also check the transaction.connection for nothing if the transaction is not nothing

http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbtransaction.connection.aspx
0
 
LVL 20

Assisted Solution

by:BuggyCoder
BuggyCoder earned 50 total points
ID: 37741668
fetched from :-
http://stackoverflow.com/questions/2011677/how-to-check-if-i-am-in-a-transaction


Depending on the nature of the SQL and its fit in the application, it may be more appropriate for you to nest transactions, ensuring a transaction is present regardless of the context of the call.

For example, you could wrap the calling code in a TransactionScope block. If there is a transaction present, this will have no real effect on the operation. If there is no transaction present, it will create a transaction and ensure the ADO.NET code participates in the transaction.

This approach only works if you're happy for the SQL to be executed as a single operation. If it should only be called as part of a larger transaction, this approach doesn't help.

To know for sure that a transaction is present, you have to check for the explicit transaction on the ADO.NET command (as you have above) and also the presence of an ambient transaction from the System.Transactions programming model using the Transaction.Current property.
0
 

Author Closing Comment

by:andreas_rafn
ID: 37744118
The code logic is very generic, thus a SQL command will sometimes be a small part of a large transaction and sometimes stand-alone. Thus the identification method put forward by code cruiser suits the purpose best.
0

Featured Post

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

724 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