Geeks With Blogs
Pradeep Loganathan Distributed

Explicit Transactions: Explicit transactions are basically ADO.net based transactions where you need to explicitly enlist & manage a transaction. If you are transaction spans a single resource you should be using this . The sample below lists a Explicit transaction.

 

using System;

using System.Data.SqlClient;

 

namespace TransTest1

{

      class Class1

      {

           

            [STAThread]

            static void Main(string[] args)

            {

                  string ConnectionString = "";

 

                  System.Data.IDbConnection Conn = new System.Data.SqlClient.SqlConnection(ConnectionString);

                  System.Data.IDbCommand Cmd = new System.Data.SqlClient.SqlCommand();

                  System.Data.IDbTransaction transaction = null;

                 

                  try

                  {

                        Conn.Open();

                        transaction = Conn.BeginTransaction();

                 

                        Cmd.Connection = Conn;             

                        Cmd.Transaction = transaction;

 

                        Cmd.CommandText = "your_insert_statement";

                        Cmd.ExecuteNonQuery();

                        transaction.Commit();

 

 

                  }

                  catch(System.Data.SqlClient.SqlException exception)

                  {

                        System.Console.WriteLine(exception.ToString());

                        transaction.Rollback();

                       

                  }

                  finally

                  {

                        Conn.Close();

                  }

 

 

            }

      }

}

 

In this case BeginTransaction method of the connection object represents the underlying database transaction and allows the developer to either commit or rollback the transaction. Since this sort of a transaction involves a single resource it does not include a lot of overhead.

Posted on Thursday, January 12, 2006 8:43 AM Transactions | Back to top

Copyright © Pradeep Loganathan | Powered by: GeeksWithBlogs.net