If you have ever used Typed DataSets, you know how fast and easy they can make database access programming. The only drawback I ever ran into while using them is when I needed to put actions associated with several different Typed DataSets into a single
Transactions and Typed DataSets are nothing new. However, when you want to place Updates to more than one database table through different Typed DataSets into a single transaction, things get a little more complicated. Hopefully, this article will help to explain that complexity and provide you with some generic class objects you can incorporate into your project with a few trivial modifications.
After searching the Web for hours and only finding a couple examples (which I couldn't get to work), I was forced to figure out my own way to accomplish this seemingly simple task. Let me tell you, the code you see here in this article took many hours to figure out and get working reliably. I am posting this article because there was so little information available on this topic on the Web that it seemed like an article on the topic would benefit the development community.
Using the Code
The Typed DataSet
Extender Class code shown in this article is added to each Typed DataSet's
TableAdapter class you want to be able to enlist in the
Partial Class file is automatically created in Visual Studio 2005 by simply double-clicking on the Typed DataSet design surface. I believe you can also right click on Typed DataSet in the Solution Explorer and choose the "View Code" option.
Unfortunately, the Auto-Generated code does not create the
Partial Class so you'll have to do that manually for each Typed DataSet you want to enlist in the
Transaction. The code shown below will be copied into each Typed DataSet's
Partial Class file created when you double-click on the Typed DataSet Design surface (the filename will be <TypedDataSetName>.vb).
Modifying the code below to extend your Typed DataSet classes is done by simply replacing the
<TypedDataSetName> tags (including the brackets
< >) in the class example shown below with the name of your Typed DataSet (the DataSet name without the XSD extension). You will need to copy the extender code below into every Typed DataSet class you want to use with a single
Transaction. You'll only need to change the
<TypedDataSetName> tags to match the name of the Typed DataSet, the rest of the code does not require any changes to work with your Typed DataSets.
Since this extender code is placed in a
Partial Class, it will not be lost when the Typed DataSet is modified or rebuilt so this effort only needs to be done once (and for any additional Typed DataSets you add to your project in the future). This is the power of the
Partial Class and is what makes it possible for you to have a
Transaction that spans across multiple Typed DataSets without invoking a Distributed
Transaction like using
Here is the Typed DataSet
NameSpace code block you'll need to paste into each Typed DataSet's
Partial Class file:
Partial Public Class <TypedDataSetName>Adapter
Call Public Function GetReturnValue() As Integer
Public Function GetAdapter() As SqlDataAdapter
Public ReadOnly Property SelectCommand() As SqlCommand
Public Property Transaction() As SqlTransaction
If (Not IsNothing((Me.Adapter.UpdateCommand.Transaction))) Then
ElseIf (Not IsNothing((Me.Adapter.InsertCommand.Transaction))) Then
ElseIf (Not IsNothing((Me.Adapter.DeleteCommand.Transaction))) Then
ElseIf (Not IsNothing((Me.Adapter.SelectCommand.Transaction))) Then
Set(ByVal value As SqlTransaction)
If (Not IsNothing(Me.Adapter.UpdateCommand)) Then
Me.Adapter.UpdateCommand.Transaction = value
Me.Adapter.UpdateCommand.Connection = value.Connection
If (Not IsNothing(Me.Adapter.DeleteCommand)) Then
Me.Adapter.DeleteCommand.Transaction = value
Me.Adapter.DeleteCommand.Connection = value.Connection
If (Not IsNothing(Me.Adapter.InsertCommand)) Then
Me.Adapter.InsertCommand.Transaction = value
Me.Adapter.InsertCommand.Connection = value.Connection
The code shown below is the DAL Interface Class required to facilitate the
Transaction sharing across multiple Typed DataSets. Just create an SqlInterface.vb file, paste all the code below into it and add it to your project - you will also need to add the required
Imports to support any
TableAdapter methods you may use in this class to retrieve data from the database.
Placing all the Typed DataSet
Fill methods in the
SqlInterface Class provides a single access point for Data Access and eliminates a lot of unnecessary complexity in your application code that would be required to handle Multi-Typed DataSet
Public Class SqlInterface
#Region " Singleton Constructor Methods "
'Private Shared reference to this Class required to provide a Singleton instance
Private Shared SqlInterfaceInstance As SqlInterface
'Provides a Singleton reference to this Class Instance
Public Shared Function GetInstance() As SqlInterface
If (IsNothing(SqlInterfaceInstance)) Then
SqlInterfaceInstance = New SqlInterface()
'Private Class Constructor called by the Shared Singleton GetInstance Method
Private Sub New()
'Do not remove this sub. This prevents someone from instantiating another
'SqlInterface Class Instance. Call the 'GetInstance()' method to get an
'instance reference to this Class. This enforces a Singleton Pattern.
#Region " Private Class Variables "
Private _UsingTransaction As Boolean = False
Private _Transaction As SqlTransaction = Nothing
Private _TransConnection As SqlConnection = Nothing
Private _TableAdapter As SqlDataAdapter = Nothing
Private _TransCommand As SqlCommand = Nothing
#Region "Transaction Methods"
'Initiates a Transaction on a Typed DataSet TableAdapter Connection. This
'transaction is automatically assigned to any other Typed DataSet TableAdapter
'initialized through this Interface Class during an active Transaction until
'the transaction is explicitly Committed or Rolled back through the SqlInterface
'Class Instance. (Note: The Private Scope of this method is intentional)
Private Function BeginTransaction(ByVal TableAdapter As SqlDataAdapter) As Boolean
'Be sure we don't already have a transaction initiated
If (Not IsNothing(_Transaction)) Then Return False
'Set Class TableAdapter Object
_TableAdapter = TableAdapter
'Set Class Connection Object
If (Not IsNothing(_TableAdapter.UpdateCommand)) Then
_TransConnection = _TableAdapter.UpdateCommand.Connection
ElseIf (Not IsNothing(_TableAdapter.InsertCommand)) Then
_TransConnection = _TableAdapter.InsertCommand.Connection
ElseIf (Not IsNothing(_TableAdapter.DeleteCommand)) Then
_TransConnection = _TableAdapter.DeleteCommand.Connection
ElseIf (Not IsNothing(_TableAdapter.SelectCommand)) Then
_TransConnection = _TableAdapter.SelectCommand.Connection
Throw New Exception("A Connection could not be established _
because no Command Object has been initialized")
'Open the DB Connection
'Set the Class Transaction Object
_Transaction = _TransConnection.BeginTransaction()
'Set the Transaction property for any instantiated Command Objects
If (Not IsNothing(_TableAdapter.UpdateCommand)) _
Then _TableAdapter.UpdateCommand.Transaction = _Transaction
If (Not IsNothing(_TableAdapter.InsertCommand)) _
Then _TableAdapter.InsertCommand.Transaction = _Transaction
If (Not IsNothing(_TableAdapter.DeleteCommand)) _
Then _TableAdapter.DeleteCommand.Transaction = _Transaction
If (Not IsNothing(_TableAdapter.SelectCommand)) _
Then _TableAdapter.SelectCommand.Transaction = _Transaction
'This method enlists Adapters used for Database Select
'Operations during an active Transaction into the current
'Transaction and Connection so the Select operation is not blocked
'by the active Update Transaction.
'This is required because the Auto-Generated TableAdpater Class
'is coded in such a way that the DataAdapter's
'SelectCommand is not initialized like the
'Update, Insert and Delete Command Objects. The SelectCommand is instead
'added to the TableAdapater internal CommandCollection
'(it's the ONLY Command object in the collection). So this is
'somewhat of a hack to work around this but at least
'it keeps all the necessary code confined to the TableAdapter
'Extension Class and this DAL Interface Class eliminating the need
'for any complicated code in the general application.
Private Sub EnlistAdapterInTransaction(ByVal Adapter As Object)
Adapter.SelectCommand.Transaction = Me._Transaction
Adapter.SelectCommand.Connection = Me._TransConnection
'Commits all DB changes performed during the Transaction and releases
'all resources and SQL DB connections.
Public Function CommitTransaction() As Boolean
If (Not IsNothing(_Transaction)) Then
'Rolls back all DB changes performed during the Transaction and
'releases all resources and SQL DB connections.
Public Function RollBackTransaction() As Boolean
If (Not IsNothing(_Transaction)) Then
'Handles cleanup for a DAL initiated transaction
Private Sub EndTransaction()
If (Not IsNothing(_TransConnection)) Then _TransConnection.Close()
If (Not IsNothing(_Transaction)) Then _Transaction.Dispose()
If (Not IsNothing(_TableAdapter)) Then _TableAdapter.Dispose()
#Region " Public Class Methods "
'Example method that executes a Select Operation on the DataBase
'with a check for an active
'transaction. If a transaction is active on the Connection,
'the SelectCommand object from
'the TableAdapter is enlisted into that Transaction
'so it is not blocked by the active Transaction.
'Note: The Typed DataSet name shown in the example is 'TdSomeData'
Public Function GetSomeDataByKey(ByVal Key As Integer) As TdSomeDataDataTable
Dim TdAdapter As New TdSomeDataAdapter
'See if an active transaction exists on the DAL and enlist
'the TableAdapter in the Transaction if there is.
If (Not IsNothing(Me._Transaction)) Then EnlistAdapterInTransaction(TdAdapter)
#Region " Public Class Properties "
'This Property provides access to an active Transaction object
'regardless of what Typed DataSet initiated the transaction.
Public ReadOnly Property Transaction(ByVal TableAdapter As SqlDataAdapter) _
If ((Me.UsingTransAction) AndAlso (IsNothing(_Transaction))) Then
'Class flag indicating a Transaction is to be used for
'subsequent DB changes until committed or rolled back.
'This flag is required to initiate a transaction on
'subsequent TableAdapter reads/writes on the underlying Database.
'The SqlInterface Class Instance (DAL Interface) will
'automatically initiate and maintain the Command, Connection and
'Transaction objects across multiple Typed DataSet TableAdapter
'interactions with the Database until the Transaction
'is explicitly Committed or Rolled back. Setting this property to True
'automatically begins a transaction when required.
Public Property UsingTransAction() As Boolean
Set(ByVal value As Boolean)
_UsingTransaction = value
Here's an example of how to use the
SQLInterface Class in your general application code:
Private SQL As SqlInterface = SqlInterface.GetInstance
Public Sub Save()
SQL.UsingTransAction = True
If (Me.HasChanges) Then
Dim TdAdapter As New MyTypedDataSetAdapter
If (SQL.UsingTransAction) Then TdAdapter.Transaction =
TdAdapter.Update(Param1, Param2, Param3)
Catch ex As Exception
That's it! If you've been avoiding the use of Typed DataSets because you didn't think you could use a single transaction for multiple Typed DataSet updates without using the costly
Transaction scope, I hope this article changes your mind. I have found Typed DataSets to be a powerful feature of the .NET Framework handling most of the underlying complexities of database interaction. Typed DataSets make updating your code easy as the underlying database schema changes with the evolution of your application. I hope you find these classes to be useful!
Points of Interest
Something I learned while developing this idea that surprised me was that the
SelectCommand object is not used by the Typed DataSet
Adapter Class. I found it even more strange that the auto-generated code only placed the
SelectCommand object in the
CommandObject collection and did not place any of the other
Command objects in the collection. The reference to each of them was assigned to their respective command objects (
- 4th May, 2008: Initial post