Click here to Skip to main content
15,894,825 members
Articles / Web Development / ASP.NET

A Beginner's Tutorial for Understanding Transactions and TransactionScope in ADO.NET

Rate me:
Please Sign up or sign in to vote.
4.80/5 (34 votes)
4 Jan 2013CPOL8 min read 154.8K   2.3K   61  
This article is a beginner's tutorial for understanding what are transactions and how can transactions be implemented using .Net framework and ADO.NET for any ASP.NET web application or any other database driven application.
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public class DataAccess
{
    string connectionString = null;

    public DataAccess(string conString)
    {
        connectionString = conString;
    }

    // This function will return the results of select query in a datatable.
    // The caller will not have to deal with connections and commands.
    public DataTable ExecuteQuery(string CommandName, CommandType cmdType, SqlParameter[] param)
    {
        DataTable table = null;

        using (SqlConnection con = new SqlConnection(connectionString))
        {
            using (SqlCommand cmd = con.CreateCommand())
            {
                cmd.CommandType = cmdType;
                cmd.CommandText = CommandName;
                if (param != null)
                {
                    cmd.Parameters.AddRange(param);
                }

                try
                {
                    con.Open();

                    using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                    {
                        table = new DataTable();
                        da.Fill(table);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }
        return table;
    }
    
    // This version of ExecuteNonQuery will work with connection and transaction
    // which are passed from outside. The callers will have to pass these
    // if they need to use ADO.NET Transactions.
    // Note: Before calling this the caller will have to open the connection and begin the transaction
    //       Also, in case of failure the rollback of transaction and disposal of connection is upto caller.
    public bool ExecuteNonQuery(string CommandName, CommandType cmdType, SqlParameter[] param, SqlConnection con, SqlTransaction trans)
    {
        bool result = false;

        using (SqlCommand cmd = con.CreateCommand())
        {
            cmd.CommandType = cmdType;
            cmd.CommandText = CommandName;
            if (param != null)
            {
                cmd.Parameters.AddRange(param);
            }

            if (trans != null)
            {
                cmd.Transaction = trans;
            }

            try
            {
                result = cmd.ExecuteNonQuery() >= 1;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        return result;
    }

    // This version of ExecuteNonQuery will work without having to pass connection and transaction
    // from outside. The callers will have to use TransactionScope if they need to use this with
    // transactions.
    public bool ExecuteNonQuery(string CommandName, CommandType cmdType, SqlParameter[] param)
    {
        bool result = false;

        using (SqlConnection con = new SqlConnection(connectionString))
        {
            using (SqlCommand cmd = con.CreateCommand())
            {
                cmd.CommandType = cmdType;
                cmd.CommandText = CommandName;
                cmd.Parameters.AddRange(param);

                try
                {
                    con.Open();

                    result = cmd.ExecuteNonQuery() >= 1;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }

        return result;
    }   
}

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.

License

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


Written By
Architect
India India

I Started my Programming career with C++. Later got a chance to develop Windows Form applications using C#. Currently using C#, ASP.NET & ASP.NET MVC to create Information Systems, e-commerce/e-governance Portals and Data driven websites.

My interests involves Programming, Website development and Learning/Teaching subjects related to Computer Science/Information Systems. IMO, C# is the best programming language and I love working with C# and other Microsoft Technologies.

  • Microsoft Certified Technology Specialist (MCTS): Web Applications Development with Microsoft .NET Framework 4
  • Microsoft Certified Technology Specialist (MCTS): Accessing Data with Microsoft .NET Framework 4
  • Microsoft Certified Technology Specialist (MCTS): Windows Communication Foundation Development with Microsoft .NET Framework 4

If you like my articles, please visit my website for more: www.rahulrajatsingh.com[^]

  • Microsoft MVP 2015

Comments and Discussions