Click here to Skip to main content
15,895,740 members
Articles / Database Development / SQL Server

Database Helper Class Library to Ease Database Operation

Rate me:
Please Sign up or sign in to vote.
3.09/5 (9 votes)
14 Apr 2007CPOL4 min read 88.2K   3K   57  
Database Helper Class Library to Ease Database Operation
///////////////////////////////////////////////////////////////////////////
// Copyright 2003-2005 Falcon Soon
//
// Author: Soon Chun Boon
// Date: 10 Jan 2004
// Description: 
// Base class for Database Interaction using MS SQL.
// Because this class implements IDisposable, derived classes shouldn't do so.
///////////////////////////////////////////////////////////////////////////

using System;
using System.Data;
using System.Data.SqlClient;

namespace DBHelper.SqlClient
{
    /// <summary>
    /// Abstract base class for Database Interaction classes using MS SQL.
    /// </summary>
    public abstract class SqlDBInteractionBase : DBInteractionBase, IDisposable
    {
        #region Class Member Declarations
            /// <summary>
            /// An instance of connection.
            /// </summary>
            protected	SqlConnection			mcnnMainConnection;

            /// <summary>
            /// Value specifies whether connection is created local.
            /// </summary>
            protected	bool					mbMainConnectionIsCreatedLocal;

            /// <summary>
            /// An instance of SQL connection provider.
            /// </summary>
            protected	SqlConnectionProvider	mobjMainConnectionProvider;

            private		bool					mbIsDisposed;
        #endregion


        /// <summary>
        /// Initializes a new instance of <see cref="DBHelper.SqlClient.SqlDBInteractionBase"/>.
        /// </summary>
        /// <remarks>
        /// When first been initialized, local connection is created.
        /// </remarks>
        public SqlDBInteractionBase()
        {
            // Initialize the class' members.
            InitClass();
        }


        /// <summary>
        /// Initializes class members.
        /// </summary>
        protected override void InitClass()
        {
            // create all the objects and initialize other members.
            base.InitClass();
            mcnnMainConnection = new SqlConnection();
            mbMainConnectionIsCreatedLocal = true;
            mobjMainConnectionProvider = null;
            mbIsDisposed = false;
        }


        /// <summary>
        /// Implements the IDispose' method Dispose.
        /// </summary>
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }


        /// <summary>
        /// Implements the Dispose functionality.
        /// </summary>
        protected virtual void Dispose(bool bIsDisposing)
        {
            // Check to see if Dispose has already been called.
            if(!mbIsDisposed)
            {
                if(bIsDisposing)
                {
                    // Dispose managed resources.
                    if(mbMainConnectionIsCreatedLocal)
                    {
                        // Object is created in this class, so destroy it here.
                        mcnnMainConnection.Close();
                        mcnnMainConnection.Dispose();
                        mbMainConnectionIsCreatedLocal = false;
                    }
                    mobjMainConnectionProvider = null;
                    mcnnMainConnection = null;
                }
            }
            mbIsDisposed = true;
        }
      
        #region Class Property Declarations
        /// <summary>
        /// Specifies the connection provider.
        /// </summary>
        public SqlConnectionProvider MainConnectionProvider
        {
            set
            {
                if(value==null)
                {
                    // Invalid value
                    throw new ArgumentNullException("MainConnectionProvider", "Null passed as value to this property which is not allowed.");
                }

                // A connection provider object is passed to this class.
                // Retrieve the SqlConnection object, if present and create a
                // reference to it. If there is already a MainConnection object
                // referenced by the membervar, destroy that one or simply 
                // remove the reference, based on the flag.
                if(mcnnMainConnection!=null)
                {
                    // First get rid of current connection object. Caller is responsible
                    if(mbMainConnectionIsCreatedLocal)
                    {
                        // Is local created object, close it and dispose it.
                        mcnnMainConnection.Close();
                        mcnnMainConnection.Dispose();
                    }
                    // Remove reference.
                    mcnnMainConnection = null;
                }
                mobjMainConnectionProvider = (SqlConnectionProvider)value;
                mcnnMainConnection = (SqlConnection)mobjMainConnectionProvider.DBConnection;
                mbMainConnectionIsCreatedLocal = false;
            }
        }

        /// <summary>
        /// Specifies the connection string.
        /// </summary>
        public override string ConnectionString
        {
            get
            {
                return (base.ConnectionString);
            }
            set
            {
                base.ConnectionString = value;
                mcnnMainConnection.ConnectionString = value;
            }
        }
        #endregion
    }
}

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
Malaysia Malaysia
Had worked as analyst programmer for 4 years. Now helping in family business but still involved actively in .Net development whenever there is a free time.

Comments and Discussions