Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2012-03-20
3
Medium Priority
?
876 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 1800 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 200 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
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…
Six Sigma Control Plans

722 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