Click here to Skip to main content
Licence CPOL
First Posted 18 Feb 2007
Views 46,444
Downloads 403
Bookmarked 22 times

Using TableAdapters with transactions

By | 18 Feb 2007 | Article
Use TableAdapters with transactions.
 
Part of The SQL Zone sponsored by
See Also

Introduction

How can we update two TableAdapters in one transaction? I really was surprised to have so much trouble with this issue, which I assumed would be a basic functionality.

I will use the Northwind database as an example. I want to add a new record to the Order and Order Details tables. I want to do this using a transaction. I do not want to add orders without order details. And, I want to use TableAdapters.

Problem

Well, the problem is that TableAdapters do not expose a Connection property or Transaction property, so how am I going to do this?! Beats me! Luckily, Reflection, Google, and Ryan Whitaker did save my day(s).

Solution

  • 0. Initialize connection, begin transaction.
  • 1. Use Reflection to access the SqlDataAdapter of the TableAdapter(s) on which you want to use the transaction.
  • 2. Set this adapter's Insert/Update/Delete Connection property to the connection which did create the transaction.
  • 3. Set this adapter's Insert/Update/Delete Transaction property to the appropriate transaction.
  • 4. Do this for each TableAdapter you need to update in the transaction.
  • 5. Commit the transaction.

In other words:

0.
conn = new SqlConnection(Properties.Settings.Default.NorthwindConnectionString);
conn.Open();
trans = conn.BeginTransaction();
1. 
public SqlDataAdapter GetAdapter(object tableAdapter)
{
    Type tableAdapterType = tableAdapter.GetType();
    SqlDataAdapter adapter = (SqlDataAdapter)tableAdapterType.GetProperty("Adapter", 
           BindingFlags.Instance | BindingFlags.NonPublic).GetValue(tableAdapter, null);
    return adapter;
}
2.
adapter.InsertCommand.Connection = trans.Connection;
adapter.UpdateCommand.Connection = trans.Connection;
adapter.DeleteCommand.Connection = trans.Connection;

3.
adapter.InsertCommand.Transaction = trans;
adapter.UpdateCommand.Transaction = trans;
adapter.DeleteCommand.Transaction = trans;

4. 
-

5. 
trans.commit();

Download the attached project for a working example.

Make sure you change this line of code to your appropriate database connection:

Properties.Settings.Default.NorthwindConnectionString = 
   @"Data Source=kenny2;Initial Catalog=Northwind;Integrated Security=True";

License

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

About the Author

Danko Greiner



Croatia Croatia

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralThank you!!! PinmemberJamesHG6:24 13 Apr '10  
GeneralThanx Pinmembermohammad7:55 16 Mar '08  
GeneralSCOPE_IDENTITY Not rolling back Pinmemberpaulvzyl0:16 31 May '07  
GeneralRe: SCOPE_IDENTITY Not rolling back PinmemberDanko Greiner1:32 31 May '07  
GeneralRe: SCOPE_IDENTITY Not rolling back Pinmemberpaulvzyl4:04 31 May '07  
Questionpartial class PinmemberBoudino3:39 23 May '07  
AnswerRe: partial class PinmemberDanko Greiner4:24 23 May '07  
GeneralRe: partial class PinmemberBoudino8:00 23 May '07  
GeneralBtw... Pinmemberfadee2:47 20 May '07  
Generaleasy way... Pinmemberfadee2:46 20 May '07  
GeneralRe: easy way... PinmemberDanko Greiner21:30 20 May '07  
GeneralRe: easy way... Pinmemberfadee0:02 21 May '07  
QuestionRe: easy way... PinmemberPreston McCormick14:55 26 Jun '07  
AnswerRe: easy way... PinmemberAlexander Myachin19:52 10 Aug '07  
GeneralI have another way... PinmemberShipcreak10:46 22 Feb '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 18 Feb 2007
Article Copyright 2007 by Danko Greiner
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid