Solved

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

Posted on 2012-03-20
3
821 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

This article will show, step by step, how to integrate R code into a R Sweave document
A short article about problems I had with the new location API and permissions in Marshmallow
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
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…

763 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

16 Experts available now in Live!

Get 1:1 Help Now