This class provides the abstract implementation for the Code Associate Data Access Block. The Abstract implementation is build upon utilizing common System.Data methods and interfacing out the Specific DBCommand using the IDbCommand interface. In doing this all specializations built on top of this class will behave in the same manor. This class is abstract and cannot be created.

Namespace:  CA.Blocks.DataAccess
Assembly:  CA.Blocks.DataAccess (in CA.Blocks.DataAccess.dll) Version: 1.0.0.0

Remarks

source code:

CopyC#
//Source code from the Code Associate C# code library, Full documentation and latest updates can be found
//@ http://www.codeassociate.com/caapi/
//===============================================================================
// Code Associate Data Access Block for .NET
// DataAccessCore.cs
// 
//===============================================================================
// Copyright (C) 2002-2010 Ravin Enterprises Ltd. 
// All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
using System;
using System.Data;
using System.Collections;
using System.Data.Common;
using System.Configuration;

namespace CA.Blocks.DataAccess 
{
    public abstract class DataAccessCore
    {
        static private bool _debugTrace = true;
        private readonly string _connectionString;

-        #region private utility methods & constructors
 
         protected string ResolveConnectionStringValue(string connectionString)
         {
             return connectionString;
         }
 
 
         protected DataAccessCore(string dataServiceName)
         {
             _connectionString = ConfigurationManager.ConnectionStrings[dataServiceName].ConnectionString;
             _connectionString = ResolveConnectionStringValue(_connectionString);
         }
 
         protected void WrapUp(IDbConnection conn, bool closeConnection)
         {
             if (closeConnection)
             {
                 if (conn != null
                     && (conn.State == ConnectionState.Open
                     || conn.State == ConnectionState.Executing
                     || conn.State == ConnectionState.Fetching)
                     )
                 {
                     conn.Close();
                 }
             }
         }
 
         //TODO make a virtual method and include a timespan on the execute time of the query
         private void TraceDBStatement(IDbCommand cmd)
         {
             System.Diagnostics.Debug.WriteLine(cmd.CommandText);
         }
 
         #endregion private utility methods & constructors

-        #region abstract methods that must me implemented
 
 
         protected abstract DbDataAdapter GetDataAdapter(IDbCommand cmd);
 
         protected abstract bool PrepCommand(IDbCommand cmd);
 
         #endregion

        protected string ConnectionString
        {
            get {return _connectionString;}
        }

-        #region ExecuteNonQuery
 
         public int ExecuteNonQuery(IDbCommand cmd)
         {
             bool closeconection = PrepCommand(cmd);
             int rowCount = cmd.ExecuteNonQuery();
             if (_debugTrace)
                 TraceDBStatement(cmd);
             WrapUp(cmd.Connection, closeconection);
             return rowCount;
         }
         #endregion ExecuteNonQuery

-        #region ExecuteDataSet
         public DataSet ExecuteDataSet(IDbCommand cmd)
         {
             DataSet ds = new DataSet();
             return (ExecuteDataSet(cmd, ds, "Results"));
         }
 
 
         public DataSet ExecuteDataSet(IDbCommand cmd, DataSet ds, string sTableNames)
         {
             // full ownership of the connection
             bool closeconection = PrepCommand(cmd);
             string[] sTableNNameArray = sTableNames.Split(',');
 
             using (DbDataAdapter theDataAdapter = GetDataAdapter(cmd))
             {
                 for(int i = 1; i < sTableNNameArray.Length; i++)
                 {
                     theDataAdapter.TableMappings.Add(sTableNNameArray[0].Trim() + Convert.ToString(i), sTableNNameArray[i].Trim());
                 }
                 theDataAdapter.Fill(ds, sTableNNameArray[0].Trim());
             }
             WrapUp(cmd.Connection, closeconection);
             if (_debugTrace)
                 TraceDBStatement(cmd);
             return (ds);
         }
 
         #endregion ExecuteDataSet

-        #region ExecuteTable
 
         public DataTable ExecuteDataTable(IDbCommand cmd)
         {
             DataSet ds = ExecuteDataSet(cmd);
             return(ds.Tables[0]);
         }
 
         #endregion ExecuteTable

-        #region ExecuteDataRow
 
         public DataRow ExecuteDataRow(IDbCommand cmd)
         {
             DataSet ds = ExecuteDataSet(cmd);
             DataRow dr = null;
             if (ds.Tables[0].Rows.Count > 0)
             {
                 dr = ds.Tables[0].Rows[0];
             }
             return(dr);
         }
 
         #endregion ExecuteDataRow

-        #region ExecuteDictionary 
 
         public IDictionary ExecuteDictionary(IDbCommand cmd)
         {
             Hashtable dictionary = new Hashtable();
             DataTable dt  = ExecuteDataTable(cmd);
             if (dt.Rows.Count > 0) 
             {
                 for (int counter = 0; counter < dictionary.Count; counter++)
                 {
                     dictionary[dt.Columns[counter].ColumnName] = dt.Rows[0].ItemArray[counter];
                 }
             }
             return (dictionary);
         }
 
         #endregion ExecuteDictionary 

-        #region ExecuteScalar
 
         public object ExecuteScalar(IDbCommand cmd)
         {
             PrepCommand(cmd);
             object rv = (cmd.ExecuteScalar());
             if (_debugTrace)
                 TraceDBStatement(cmd);
             cmd.Connection.Close();
             return rv;
         }
 
         #endregion ExecuteScalar

-        #region ExecuteReader
         // the connection is held open until the reader is closed..
         public IDataReader ExecuteReader(IDbCommand cmd)
         {
             PrepCommand(cmd);
             return(cmd.ExecuteReader(CommandBehavior.CloseConnection));
         }
 
         #endregion ExecuteReader

    }
}

Inheritance Hierarchy

See Also