Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Provider Factory

0.00/5 (No votes)
12 Feb 2010 1  
Providers factory provides us indepandent database access where we can connect to any database sources(SQLServer,DB2,Oracle,MS-Access).Provider

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

Providers factory provides us indepandent database access where we can connect to any database sources(SQLServer,DB2,Oracle,MS-Access).
Provider Factory allows programmers to write their own implementation for accessing database.

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;

namespace DatabaseProvider
{
    /// <summary>
    ///
    /// Design Pattern: Factory.
    /// </summary>
    public abstract class Database
    {
        private string connectionString;       
        private DbProviderFactory dbProviderFactory;
        private string schema;
        private string dataProvider;
        private bool status;

        protected Database() { }    

        public DbProviderFactory DbProviderFactory
        {
            get { return dbProviderFactory; }
        }      
        public string ConnectionString
        {
            get { return connectionString; }
            set { connectionString = value; }
        }       
        public string Schema
        {
            get { return schema; }
        }

        public string DataProvider
        {
            get { return dataProvider; }
        }
           
        public DbConnection GetConnection()
        {
            DbConnection newConnection = null;
            try
            {
                try
                {
                       ProviderFactory = DbProviderFactories.GetFactory(this.ProviderType);
                       dbConnection = ProviderFactory.CreateConnection();
                       dbConnection.ConnectionString = this.ConnectionString;                    
                }
                catch
                {
                    throw;
                }
            }
            catch
            {
                if (newConnection != null)
                    newConnection.Close();

                throw;
            }

            return newConnection;
        }

        public DbCommand CreateCommand()
        {
            return dbProviderFactory.CreateCommand();
        }

        public DbCommand CreateCommand(string sQueryString, DbConnection connection)
        {
            try
            {
                // Create the DbCommand.
                DbCommand command = this.CreateCommand();
                command.CommandText = sQueryString;
                command.Connection = connection;

                return command;
            }
            catch
            {
                throw;
            }
        }      

        public DbDataAdapter CreateDataAdapter()
        {
            return dbProviderFactory.CreateDataAdapter();
        }
        public DbDataAdapter CreateDataAdapter(string sQueryString, DbConnection connection)
        {
            try
            {
                // Create the DbCommand.
                DbCommand command = this.CreateCommand();
                command.CommandText = sQueryString;
                command.Connection = connection;

                // Create the DbDataAdapter.
                DbDataAdapter adapter = this.CreateDataAdapter();
                adapter.SelectCommand = command;
               
                return adapter;
            }
            catch
            {
                throw;
            }
        }

        private DbCommandBuilder CreateCommandBuilder()
        {
            return dbProviderFactory.CreateCommandBuilder();
        }

        public DbCommandBuilder CreateCommandBuilder(DbDataAdapter dbDA)
        {
            DbCommandBuilder dbCB = this.CreateCommandBuilder();
            dbCB.DataAdapter = dbDA;

            return dbCB;
        }       
    }
}

web.config
<add key="Connectionstring" value="database=local;user id=sa;pwd=sa;initial catalog=northwind"/>
<add key="SQLProvider" value="System.Data.SqlClient"/>
<add key="OledbProvider" value="System.Data.OleDb"/>
<add key="Db2Provider" value="IBM.Data.DB2"/>
<add key="OracleProvider" value="System.Data.OracleClient"/>

aspx.cs
private Database DB;
private DbDataAdapter SQLDA;
private DbCommandBuilder SQLDB;
private DbConnection Conn;
private DBCommand cmd;

//pass the connection string and provider type to create database connection
DB.Connectionstring=Configuration.ConfigurationManager.AppSettings["Connectionstring"].ToString();
DB.DataProvider=Configuration.ConfigurationManager.AppSettings["SQLProvider"].ToString();

//create database connection
Conn = DB.GetConnection();
Conn.Open();

//creating Data Adapter
SQLDA=DB.CreateDataAdapter("select * from emp",Conn);

//Create Command Builder
SQLDB=DB.CreateCommandBuilder(SQLDA);

//Create Command
cmd=DB.CreateCommand("select * from emp",Conn);

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here