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
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.
SQL Server - System.Data.SqlClient
Oracle - System.Data.OracleClient
OleDb - System.Data. OleDb
Odbc - System.Data. Odbc
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
Using the code
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
Dot Net 2.0
The Web.config file should be having the following keys in the appSettings section.
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.
When an DBHelper object is created, a DbProviderFactory object is created based on the data provider specified on the Web.config file.
DbProviderFactory oFactory = DbProviderFactories.GetFactory(PROVIDER);
The DbProviderFactory object is disposed.
oFactory = null;
Connection Related Methods
DbConnection object is created based on the connection string specified in the Web.config file with DbProviderFactory object.
DbConnection oConnection = oFactory.CreateConnection();
Connection is closed and the DbConnection object gets disposed.
Parameter Related Methods
A structure has been created to define the parameter details like
The structure provides two constructors
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.
public void TransactionHandler(TransactionType veTransactionType)
Provides a set of overloaded methods for
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.