Description
ADO .NET provides us with SQL Server and OLE Db providers. I have seen many programmers code directly using the classes in the System.Data.SqlClient namespace while connecting to the SQL Server and using the classes in the System.Data.OleDb namespace while connecting to other databases like MS Access. They do this mainly because of the System.Data.SqlClient classes are optimized for SQL Server. Good. But what if somebody wants to switch databases? Say you initially thought of using MS Access and then later decided to switch to SQL Server. Now what? Do you rewrite all your business logic and database layer classes again using the SqlClient provider? Well... You will have to... if you need performance.
What if you had some high-level interface that acted as a wrapper over ADO .NET that totally abstracted the database? It would be great! Right? In that case, you will not have to rewrite your classes to switch the providers. Just tell the wrapper to switch the underlying provider at runtime.
That is exactly what my Sql Wrapper does. It provides you a JDBC like Wrapper on top of ADO. NET. The underlying database can be easily switched by changing a few configuration lines in the Application Configuration file. That's all. In addition, it also provides you with connection pooling that can speed up your application to a large extent. The pooling parameters can also be changing using the same configuration file.
The Sample Program:
The zip file comes with the wrapper DLL (KishoreSql.DLL) and a sample program (KishoreSqlSample.EXE). It also contains an MS Access DB (KishoreAccessDb.mdb) that is used by the sample program. In addition, it also contains all the source files. You can choose to have your database. The current sql wrapper supports Access and SQL Server but it can be easily extended for other databases. If you plan to use your own database for the sample, you are free to do so. The sample needs a database with two tables
Table 1: Role
Fields: Id (AutoIncrement)
Name (VarChar) 50
Desc (VarChar) 50
Table 1: Permission
Fields: Id (AutoIncrement)
Name (VarChar) 50
The sample form is shown below: It consists of two DB Grid controls. One to display roles and others for permissions. There are buttons for adding and deleting roles and permissions.

How to use SQL Wrapper:
The basic service for providing connection management is done by an object called DbConnectionManager. This is a singleton class and only one instance of this exists. As a part of its initialization, this object reads the driver and connection pooling configuration that are set in the configuration file. A sample configuration for the SQL Server and MS access is shown below.
- <DbConnectionManager>
- <!-- SQL Server configuration -->
- <ConnectionPool name="DemoPool">
- <DriverClass>Kishore.Sql.SqlServer.Driver</DriverClass>
- <ConnectionString>Persist Security Info=False;Initial Catalog=KishoreProject;Data Source=kishore;User ID=kishorch;Password=krckrc;Connect Timeout=10;</ConnectionString>
- <MinimunConnections>2</MinimunConnections>
- <MaximumConnections>10/MaximumConnections>
- <ConnectionTimeout>60</ConnectionTimeout>
- <ConnectionReaperDelay>60</ConnectionReaperDelay>
- </ConnectionPool>
- <!-- MS Access configuration -->
- <ConnectionPool name="SamplePool">
- <DriverClass>Kishore.Sql.OleDb.Driver</DriverClass>
- <ConnectionString>Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Learning\ADONET\Kishore\KishoreSqlSample\KishoreAccessDb.mdb</ConnectionString>
- <MinimunConnections>2</MinimunConnections>
- <MaximumConnections>10</MaximumConnections>
- <ConnectionTimeout>60</ConnectionTimeout>
- <ConnectionReaperDelay>60</ConnectionReaperDelay>
- </ConnectionPool>
- </DbConnectionManager>
The pool is configured with a Driver class to use during runtime. This driver must implement the Kishore.Sql.IDriver interface. The driver acts as a factory of connections. It is possible to configure more than one pool. For example you could have a pool of MSAccess connections and also one or more pools of SqlServer connections side by side. The DBConnectionManager handles all the pools. The pools are identified by a unique name.
A Connection can be retrieved from the DbConnectionManager by using the GetConnection() method as shown below:
- IConnection conn = DbConnectionManager.GetInstance().GetConnection("SamplePool");
The code snippet below shows the IConnection interface
- public interface IConnection {
- IDbTransaction BeginTransaction();
- IDbTransaction BeginTransaction(System.Data.IsolationLevel iso);
- IStatement CreateStatement(string sql);
- IPreparedStatement PrepareStatement(string sql);
- ICallableStatement PrepareCall(string sql);
- void Open();
- void Close();
- void ReOpen();
- ConnectionState State { get; }
- }
Join the conversation! Your thoughts help the community grow.