Shaikh Sonny Aman’s Blog

previously www.mailtoaman.com

ASP.net C#: Easily switch between SQL server database and access by an abstraction layer

Posted on | April 20, 2008 |

Problem:

You are developing an asp.net application which runs on MS SQL server on the back end. You need to show a demo to a person who does not have MS SQL Server. You have to create or export to a access database.

or

Learning ASP.net,C# newly :)

Challenges:

You have to choose SqlConnection ,SqlCommand or OleDbConnection, OldeDbCommand for SQL Server or access respectively. Either you have to replace all or have to find out a way around for this.

My Way Around:

Creating an abstraction layer. I know there are many of such classes or modules present on the net. I searched on codeproject.com and find some nice and great data abstraction layers. If you are looking for a complete database abstraction layer, this post might not help you. Rather, it will help you showing how to develop a simple abstraction layer for different type of connection.

How to use:

DALFactory.StrConnection = ConfigurationManager.AppSettings["conn_string"];// How i’m using

DALFactory.DALType = DALFactory.SQL;// or ACCESS

DAL dal = DALFactory.GetDal();

dal.SetSql(”Select * from student where id=@id”);
dal.AddWithValue(”@id”,1);
IDataReader reader = dal.ExecuteReader();

Thats it :)

Here are the file:

dal.cs
dalfactory.cs
dalsql.cs
dalaccess.cs

PS: I think the naming is not proper.. DALAccesss should DALOle

Comments

Leave a Reply