Solved

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

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

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.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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 …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

785 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