Click here to Skip to main content
Licence CPOL
First Posted 23 Feb 2008
Views 39,425
Downloads 2,460
Bookmarked 43 times

DbHelper for DotNet 2.0 using DbProviderFactory

By | 27 Feb 2008 | Article
DbHelper comes with methods to manipulate data using the features of DbProviderFactory.

Introduction

This document covers

         
       1. Overview of DbProviderFactory [in the Background section]
       2. DBHelper class to implement DbProviderFactory concepts 
       3. Pre-requisites for DBHelper class
       4. Overview of DBHelper class

In addition, this include source codes for


       1. DBHelper class
       2. EmployeeDL [DataLayer class] with the implementation

Background

Since DotNet evolved, ADO.Net have been getting improvized by trying different approaches. To communicate with different databases ADO.Net have been providing a set of numerous data providers which can be readily accessed under the “System.Data” namespace.

For example

       SQL Server      	- System.Data.SqlClient
       Oracle		- System.Data.OracleClient
       OleDb		- System.Data. OleDb
       Odbc		- System.Data. Odbc
       etc…

In the real world scenario, there are chances of changing databases. As per the current situation, developers use to write code based on a specific data provider, say SQL. But if the database has to be changed to Oracle, the code must be rewritten to support the Oracle data provider, which is really tedious. To overcome this situation ADO.Net has come up with a generic solution by providing a new namespace under “System.Data” which is “System.Data.Common”.

Generalized versions of all data providers are available under this namespace, namely

       •	DbCommand 
       •	DbCommandBuilder 
       •	DbConnection 
       •	DbDataAdapter 
       •	DbDataReader 
       •	DbException 
       •	DbParameter 
       •	DbTransaction

Using the code

DBHelper Class

DBHelper class is created with the intension to reduce the coding effort by using the DbProvider Factory concepts. This class provides a set of methods for

	
       •	ExecuteNonQuery 
       •	ExecuteScalar
       •	ExecuteReader
       •	DataAdapter

Pre-requisites

Dot Net 2.0

Settings

The Web.config file should be having the following keys in the appSettings section.

       // Include the lines in the Web.config file with the required details with opening and closing tags
       add key="DATA.PROVIDER" value="System.Data.SqlClient"
       add key="DATA.CONNECTIONSTRING" value="data source=ServerName;initial catalog =DatabaseName; user id =UserId; pwd =Password;"

       DATA.PROVIDER		- To define the Provider.
       DATA.CONNECTIONSTRING	- To define the Connection string.

Overview of DBHelper class

Below mentioned are some of the methods available in the DBHelper class. This helps to get an overview of the working of DBHelper class and its methods.

Constructor


DBHelper()

When an DBHelper object is created, a DbProviderFactory object is created based on the data provider specified on the Web.config file.

       // Creating DbProviderFactory object based on the provider given in Web.config 
       DbProviderFactory oFactory = DbProviderFactories.GetFactory(PROVIDER);

Destructor


~DBHelper()

The DbProviderFactory object is disposed.

       // Disposing DbProviderFactory object
       oFactory = null;

Connection Related Methods


EstablishFactoryConnection()

DbConnection object is created based on the connection string specified in the Web.config file with DbProviderFactory object.

       // Creating DbConnection object based on the connection string given in Web.config 
       DbConnection oConnection = oFactory.CreateConnection();

CloseFactoryConnection()

Connection is closed and the DbConnection object gets disposed.

       // Closing the connection and disposing the DbConnection object
       oConnection.Close();
       oConnection.Dispose();

Parameter Related Methods

A structure has been created to define the parameter details like

       •	ParamName
       •	ParamValue
       •	ParamDirection

The structure provides two constructors

       // Constructors for the structure
       public Parameters(string Name, object Value, ParameterDirection Direction)
       public Parameters(string Name, object Value)

Transaction Related Methods

An enumerator has been created to define TransactionType which could be either

       •	Open	- Begin Transaction 
       •	Commit	- Commit Transaction
       •	Rollback - Rollback Transaction

TransactionHandler method handles the transaction based on the defined Transaction type.

       // Method to handle Transaction
       public void TransactionHandler(TransactionType veTransactionType)

Execute Methods

Provides a set of overloaded methods for

       // Overloaded methods avaliable for
       •	ExecuteScalar
       •	ExecuteReader
       •	ExecuteNonQuery
       •	DataAdapter

There are many overloaded methods for the above mentioned methods which are currently not mentioned here. You can get a better overview of these methods and their implementation in the example class [EmployeeDL] attached along with this document.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Shyam SS

Web Developer
Infosys Technologies Limited
India India

Member

Have 3.5 years of experience in IT industry, mostly into ASP.Net, C#, SQL Server.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionSqlserverCe PinmemberMember 79840020:54 29 May '12  
QuestionGood work Pinmemberrohit kakria21:50 27 Apr '12  
GeneralMy vote of 5 Pinmemberlaopei22:02 26 May '11  
GeneralMy vote of 5 Pinmembersridharceg6:22 29 Nov '10  
GeneralHi Siam Pinmembersognant10:05 18 Feb '10  
GeneralBulk copy PinmemberSumit Thapar18:17 7 Jan '10  
GeneralParameters (SQLServer and MYSQL) Pinmemberchicodrummer20:22 22 Mar '08  
GeneralRe: Parameters (SQLServer and MYSQL) PinmemberKang Chian Gim17:07 18 Mar '11  
GeneralShyam PinmemberFabio Galante Mans14:14 9 Mar '08  
Shyam you can to sender the others class for me?
I need to learn use MVC.
 
Entity
DL
 
Fabio
fgamans@gmail.com
GeneralWonderful article Pinmembersujeeth.maroli14:58 25 Feb '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 27 Feb 2008
Article Copyright 2008 by Shyam SS
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid