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

A Handy Code to Get Your Project Connected to Any Database by ADO.NET

Rate me:
Please Sign up or sign in to vote.
3.78/5 (10 votes)
16 Jul 2009CPOL5 min read 48.1K   340   62  
Purely object oriented ADO code component to get your project running, no matter what database you are using at the backend. Specialised for SQL Server.
using System;
using System.Data;
using System.Data.Common;
using System.Data.Sql;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Configuration;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Top Layer For Accessing Data
/// </summary>
public sealed class DataAccess
{
    #region Data Members And Objects Declaration
    readonly string connectionString;
    readonly string provider;
    FactoryGoverner governer;
    SQLFactory sqlFactory = new SQLFactory();
    ProviderAgnosticFactory pAFactory = new ProviderAgnosticFactory();
    SqlDataReader dR;
    DbDataReader dbDR;
    #endregion

    #region Constructor For DataAccess
    public DataAccess(string configSectionName)
    {
        connectionString = WebConfigurationManager.ConnectionStrings[configSectionName].ToString();
        provider = WebConfigurationManager.ConnectionStrings[configSectionName].ProviderName;
    }

    public DataAccess(string conStr, string providerName)
    {
        connectionString = conStr;
        provider = providerName;
    }
    #endregion

    #region Private Methods
    private void PrepareEnvironment(QueryType queryType)
    {
        try
        {
            switch (provider)
            {
                case "System.Data.SqlClient":
                    governer = sqlFactory;
                    governer.InvokeFactory(FactoryCodes.SQLClient, connectionString, queryType);
                    break;
                case "System.Data.OracleClient":
                    governer = pAFactory;
                    governer.InvokeFactory(FactoryCodes.OracleClient, connectionString, queryType);
                    break;
                case "System.Data.OleDb":
                    governer = pAFactory;
                    governer.InvokeFactory(FactoryCodes.OleDB, connectionString, queryType);
                    break;
                case "System.Data.Odbc":
                    governer = pAFactory;
                    governer.InvokeFactory(FactoryCodes.ODBC, connectionString, queryType);
                    break;
                default:
                    throw new UnsupportedFactoryException("Database Is Unsupported");
            }
        }
        catch (NullReferenceException ex)
        {
        }
        catch (UnsupportedFactoryException ex)
        {
        }
    }
    #endregion
    #region Data Access Public Methods
    public DataSet GetDataSet(QueryType queryType, string query, params AgnosticParameter[] argumentsIn)
    {
        PrepareEnvironment(queryType);
        return governer.GetDataSet(query, argumentsIn);
    }

    public SqlDataReader GetSQLReader(QueryType queryType, string query, params AgnosticParameter[] argumentsIn)
    {
        PrepareEnvironment(queryType);
        try
        {
            if (provider != "System.Data.SqlClient")
                throw new UnsupportedFactoryException("Method Not For Non SQL Client");
            else
                return (SqlDataReader)governer.GetReader(query, argumentsIn);
        }
        catch (InvalidCastException ex)
        {

        }
        return dR;
    }

    public DbDataReader GetReader(QueryType queryType, string query, params AgnosticParameter[] argumentsIn)
    {
        PrepareEnvironment(queryType);
        try
        {
            if (provider == "System.Data.SqlClient")
                throw new UnsupportedFactoryException("Method Not For SQL Client");
            else
                return (DbDataReader)governer.GetReader(query, argumentsIn);
        }
        catch (InvalidCastException ex)
        {
        }
        return dbDR;
    }

    public object GetScalarResult(QueryType queryType, string query, params AgnosticParameter[] argumentsIn)
    {
        PrepareEnvironment(queryType);
        return governer.GetScalarResult(query, argumentsIn);
    }

    public int GetNonQueryResult(QueryType queryType, string query, params AgnosticParameter[] argumentsIn)
    {
        PrepareEnvironment(queryType);
        return governer.GetNonQueryResult(query, argumentsIn);
    }

    public DataSet GetOutParameters(QueryType queryType, string sp_Name, ref AgnosticParameter[] argumentsOut, params AgnosticParameter[] argumentsIn)
    {
        PrepareEnvironment(queryType);
        return governer.GetOutParameters(sp_Name, ref argumentsOut, argumentsIn);
    }
    #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
Software Developer HCL Technologies Limited
India India
Rahul has worked on several technologies in Microsoft's technology stack. He has been in field of software developemnt for two years. He started with ASP.net and graduated to Windows application develeopment. His technology experience includes C#, LINQ, T-SQL,T-SQL, XML, PL\SQL, C, C++, VB, WebServices..

Apart from all these he is working on some of the most recent technology offerings such as Windows Azure, SQL Azure, SQL Azure Sync framework, MS Dallas, Powershell etc.

His goal of knowledge is to possess knowledge that is sky high and earth deep.

Comments and Discussions