Click here to Skip to main content
6,629,885 members and growing! (23,809 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » .NET Framework » General     Intermediate License: The Code Project Open License (CPOL)

Transaction Aware Table-adapters in .NET 2.0

By Mike Pagel

A quick and elegant way to add transaction capabilities to your table-adapters
C# 2.0, Windows, .NET 2.0VS2005, Dev
Posted:31 Aug 2007
Updated:5 Dec 2007
Views:21,727
Bookmarked:26 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
14 votes for this article.
Popularity: 5.18 Rating: 4.52 out of 5

1

2
1 vote, 7.1%
3
2 votes, 14.3%
4
11 votes, 78.6%
5

Introduction

Much has been said about missing support for atomic transactions in .NET 2.0 when using table adapters that get generated by the dataset designer. This article provides a ready-to-use base-class that by injecting it into a table-adapter's inheritance line equips it with transaction capabilities.

Discussion of Approaches

One way to get around the aforementioned limitations is to configure your server to have DTC (Distributed Transaction Coordination) enabled. In that case, you can use TransactionScope instances guarding actions like modifications through table adapters. However, if you don't want to or cannot change the server's configuration, TransactionScope is not an option.

Others on The Code Project and other sites have explained how to enable transactions for table adapters. The basic idea always is to attach an SqlTransaction to all commands of a table-adapter. If multiple table-adapters are used, share the same transaction across all commands of all adapters. What makes this a little difficult is that a generated table-adapter keeps its SqlDataAdapter property Adapter private, but transaction code requires modifying the property.

There are several approaches to solve this issue. For instance Avoiding DTC with Typed DataSets and SQL Server 2000 suggests adding transaction code in the form of partial classes. As the partial extension lives in the same scope as the original table-adapter, it can also access its private properties. The major disadvantage here is that partial classes have to be created for each generated table adapter.

A much more elegant approach is mentioned in this post (unfortunately in German): use reflection to get your hands at those properties. The article also mentions a small detail I found very interesting: when editing a table-adapter in designer-mode, you are actually able to change its base class! So instead of the default base-class Component you can fill in your own base-class. If you now provide such a base-class that is derived from Component and implements the transaction stuff through reflection, usage becomes simple and elegant. And this is exactly what I did: putting together such a handy base-class called TransactionSupport.

Using the Code

To take advantage of the base class is very simple: First download the class source code and drop it into your project. Then, for all tables in your dataset that require transaction support, change the table adapter's base-class like this:

  1. Select the table-adapter (not the table) in the dataset designer, such that it becomes visible in the properties view.

    Screenshot - ta-properties.png

  2. Change the property BaseClass from System.ComponentModel.Component to the class BizWiz.TransactionSupport I provided like is shown in the screenshot.

And that's it. Do this for each table adapter required. Then you can write code of the following pattern:

// begin transaction and share it among all participating table-adapters:
customerTableAdapter.BeginTransaction();
orderTableAdapter.Transaction = customerTableAdapter.Transaction;

// now start the modifications:
try
{
    // do modifications here, throw exceptions if things go wrong...
    // ...

    // ok, all is well, commit transaction:
    customerTableAdapter.CommitTransaction();
}
catch( Exception e )
{
    // if anything went wrong, roll-back transaction
    customerTableAdapter.RollbackTransaction();
}

You can choose any of the participating table-adapters to perform commit or rollback, but I make it good practice to have one dedicated transaction master, that is doing it all: BeginTransaction(), CommitTransaction()and RollbackTransaction().

History

Date Comment
2007-SEP-15 Uploaded modified version of base that looks for Connection property of generated table adapters in public as well as non-public scope, as this seems to differ throughout installations.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Mike Pagel


Member

Occupation: Software Developer (Senior)
Company: BMW AG
Location: Germany Germany

Other popular .NET Framework articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 24 of 24 (Total in Forum: 24) (Refresh)FirstPrevNext
GeneralHi all, Pinmemberbruce2willis8:52 23 Jun '09  
GeneralThank You PinmemberGeorge_Botros6:52 19 Mar '09  
GeneralIf anyone is looking for a VB version of this... Pinmemberegbdfine8:03 29 Jan '09  
Generalweird ... Pinmembertomtom19803:30 11 Nov '08  
GeneralRe: weird ... PinmemberMike Pagel3:53 11 Nov '08  
QuestionHelp please on MySql + C# PinmemberSeuss2:58 18 Sep '08  
AnswerRe: Help please on MySql + C# PinmemberTim@Kin22:57 15 Dec '08  
QuestionHelp please on MySQL and C# PinmemberSeuss1:01 18 Sep '08  
AnswerRe: Help please on MySQL and C# PinmemberMike Pagel1:34 18 Sep '08  
QuestionRe: Help please on MySQL and C# PinmemberSeuss2:07 18 Sep '08  
GeneralWorks beautifully PinmemberNeedTechHelp12:44 13 Aug '08  
Generalpublic vs. private scope on connection PinmemberJosh Ryon11:31 15 Jul '08  
GeneralTransaction with strongly typed dataset and multiple table adapters PinmemberAnil Pandya0:43 7 Jan '08  
GeneralRe: Transaction with strongly typed dataset and multiple table adapters PinmemberMike Pagel1:19 7 Jan '08  
GeneralRe: Transaction with strongly typed dataset and multiple table adapters PinmemberAnil Pandya18:48 7 Jan '08  
GeneralConnection is Public PinmemberTroytTheCodeProject14:21 12 Sep '07  
AnswerRe: Connection is Public [modified] PinmemberMike Pagel21:26 12 Sep '07  
GeneralRe: Connection is Public Pinmembertmv196:12 13 Sep '07  
AnswerRe: Connection is Public PinmemberMike Pagel0:28 15 Sep '07  
GeneralThe problem with this approach PinmemberMuhahahahahahahahahahahaha23:56 10 Sep '07  
AnswerRe: The problem with this approach PinmemberMike Pagel4:16 11 Sep '07  
GeneralRe: The problem with this approach PinmemberKarel Kral4:49 17 Sep '07  
GeneralGood idea, some suggestions PinmemberKarel Kral2:05 4 Sep '07  
AnswerRe: Good idea, some suggestions PinmemberMike Pagel6:23 9 Sep '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 5 Dec 2007
Editor: Deeksha Shenoy
Copyright 2007 by Mike Pagel
Everything else Copyright © CodeProject, 1999-2009
Web22 | Advertise on the Code Project