Click here to Skip to main content
14,981,456 members
Articles / Database Development
Article
Posted 11 Oct 2013

Tagged as

Stats

10.2K views
1 bookmarked

Provider Factory

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
11 Oct 2013CPOL2 min read
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

ASP.NET Community
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
Group type: Collaborative Group

756 members


Comments and Discussions

 
-- There are no messages in this forum --