Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Database Helper Class Library to Ease Database Operation

, 14 Apr 2007 CPOL
Database Helper Class Library to Ease Database Operation
article_demo.zip
DBHelperQuickRelationshipSamples
DBHelperQuickRelationshipSamples
App.ico
DBHelperQuickRelationshipSamples.csproj.user
DBHelperQuickRelationshipSamples.suo
DBHelperQuickSelectSamples
DBHelperQuickSelectSamples
DBHelperQuickSelectSamples.csproj.user
DBHelperQuickSelectSamples.suo
DBHelperQuickHelperSamples
DBHelperQuickHelperSamples
App.ico
DBHelperQuickHelperSamples.csproj.user
DBHelperQuickHelperSamples.suo
DBHelperQuickInsUpdDelSamples
DBHelperQuickInsUpdDelSamples
DBHelperQuickInsUpdDelSamples.csproj.user
DBHelperQuickInsUpdDelSamples.suo
article_src.zip
Microsoft.ApplicationBlocks.ExceptionManagement.Interfaces
Microsoft.ApplicationBlocks.ExceptionManagement.Interfaces.csproj.user
Microsoft.ApplicationBlocks.ExceptionManagement.suo
DbHelper
DBHelper.chm
DBHelper.csproj.user
DbHelper.ndoc
DBHelper.suo
Microsoft.ApplicationBlocks.ExceptionManagement
ExceptionManagerText.xsx
Microsoft.ApplicationBlocks.ExceptionManagement.csproj.user
Microsoft.ApplicationBlocks.suo
///////////////////////////////////////////////////////////////////////////
// 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)

Share

About the Author

falconsoon

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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141216.1 | Last Updated 14 Apr 2007
Article Copyright 2007 by falconsoon
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid