Click here to Skip to main content
13,141,429 members (26,338 online)
Click here to Skip to main content
Articles » Database » Database » General » Downloads


54 bookmarked
Posted 25 Oct 2004

Easier Database Transactions - Extending the Using Statement to Perform Automatic Database Transactions

Any user who works with database updates uses transactions. Transactions in ADO.NET are done using a transaction object, and a try..catch, but there is an easier, one might even say a more C# way to handle database transactions.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
using System.IO;

namespace Demo {
	public class Form1 : System.Windows.Forms.Form {
		private System.ComponentModel.Container components = null;

		public Form1() {

		protected override void Dispose( bool disposing ) {
			if( disposing ) {
				if (components != null) {
			base.Dispose( disposing );

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
      this.button1 = new System.Windows.Forms.Button();
      this.button2 = new System.Windows.Forms.Button();
      this.chckThrow = new System.Windows.Forms.CheckBox();
      // button1
      this.button1.Location = new System.Drawing.Point(16, 16);
      this.button1.Name = "button1";
      this.button1.TabIndex = 0;
      this.button1.Text = "Normal";
      this.button1.Click += new System.EventHandler(this.button1_Click);
      // button2
      this.button2.Location = new System.Drawing.Point(16, 48);
      this.button2.Name = "button2";
      this.button2.TabIndex = 1;
      this.button2.Text = "Object";
      this.button2.Click += new System.EventHandler(this.button2_Click);
      // chckThrow
      this.chckThrow.Location = new System.Drawing.Point(128, 16);
      this.chckThrow.Name = "chckThrow";
      this.chckThrow.Size = new System.Drawing.Size(128, 24);
      this.chckThrow.TabIndex = 2;
      this.chckThrow.Text = "Throw Exception";
      // Form1
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 266);
      this.Name = "Form1";
      this.Text = "Form1";
      this.Load += new System.EventHandler(this.Form1_Load);


		static void Main() {
			Application.Run(new Form1());

    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Button button2;
    private System.Windows.Forms.CheckBox chckThrow;

    private OleDbConnection _DB = new OleDbConnection();

    private void Form1_Load(object sender, System.EventArgs e) {
      _DB.ConnectionString =
        // ..\.. is source dir
        + "Data source=" + new FileInfo(Application.ExecutablePath).Directory + @"\..\..\demo.mdb";

    private void button1_Click(object sender, System.EventArgs e) {
      using (OleDbTransaction xTx = _DB.BeginTransaction()) {
        try {
          IDbCommand xCmd = _DB.CreateCommand();
          xCmd.Transaction = xTx;
          xCmd.CommandText = "Insert into Person (Name, Telephone) values ('Normal One', '(412) 555-1212')";    
          if (chckThrow.Checked) {
            throw new Exception("Test Exception");
          xCmd.CommandText = "Insert into Person (Name, Telephone) values ('Normal Two', '(423) 555-1212')";    
        } catch (Exception xException) {
          throw xException;

    private void button2_Click(object sender, System.EventArgs e) {
      using (Transaction xTx = new Transaction(_DB)) {
        IDbCommand xCmd = _DB.CreateCommand();
        xCmd.Transaction = xTx.DBTransaction;
        xCmd.CommandText = "Insert into Person (Name, Telephone) values ('Object One', '(412) 555-1212')";    
        if (chckThrow.Checked) {
          throw new Exception("Test Exception");
        xCmd.CommandText = "Insert into Person (Name, Telephone) values ('Object Two', '(423) 555-1212')";    


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Chad Z. Hower, a.k.a. Kudzu
"Programming is an art form that fights back"

Formerly the Regional Developer Adviser (DPE) for Microsoft Middle East and Africa, he was responsible for 85 countries spanning 4 continents and 10 time zones. Now Chad is a Microsoft MVP.

Chad is the chair of several popular open source projects including Indy and Cosmos (C# Open Source Managed Operating System).

Chad is the author of the book Indy in Depth and has contributed to several other books on network communications and general programming.

Chad has lived in Canada, Cyprus, Switzerland, France, Jordan, Russia, Turkey, and the United States. Chad has visited more than 60 countries, visiting most of them several times.

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170915.1 | Last Updated 25 Oct 2004
Article Copyright 2004 by Chad Z. Hower aka Kudzu
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid