Click here to Skip to main content
13,148,136 members (68,799 online)
Click here to Skip to main content
Articles » Database » Database » ADO.NET » Downloads

Stats

115.7K views
2K downloads
60 bookmarked
Posted 4 Jan 2013

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

, 4 Jan 2013
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)

Share

About the Author

Rahul Rajat Singh
Software Developer (Senior)
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

You may also be interested in...

Pro
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170924.1 | Last Updated 5 Jan 2013
Article Copyright 2013 by Rahul Rajat Singh
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid