Solved

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

Posted on 2012-03-20
3
849 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
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.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

730 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