Data Access Layer

Download

Overview

This data access layer is a Microsoft .Net 2.0 object library that helps abstract away the data access part of any .Net application.

 

The main benefits of using this access layer:

 

  • Facilitates good data access practices
  • Enforces C.R.U.D database operations
  • Allows you to program against one object model for any database vendor
  • Improves productivity by reducing the amount of code written
  • Improves maintainability by centralizing all data access code

 

Class Diagram

 

Class Diagram

 

 

Example of Data Access

 

This example shows how to use the data access layer to return records from the AdventureWorks database that comes with Microsoft SQL Server 2005.

 

First we create our DAL object, we are using MS SQL Server so we use the DALMSSQL object.

 

String connectionString = @”Data Source=LEEMCE2005\SQLEXPRESS;Initial Catalog=AdventureWorks;Integrated Security=True”;

DALMSSQL dal = new DALMSSQL(connectionString, DataAccess.ConnectStringType.String, DataAccess.ApplicationType.Windows);

 

In this constructor we tell the DALMSSQL object that we are passing it a raw connection string and we are accesing it from a Windows application.

 

Next we need to create our parameter objects to pass to our stored procedure.

           

Parameters parameters = new Parameters();

Parameter managerId = new Parameter(“@ManagerID”, “197″);

parameters.Add(managerId);

 

Now we have a collection of parameters we can call the stored procedure as follows

 

SqlDataReader reader = (SqlDataReader)dal.ReadRecords(“uspGetManagerEmployees”, parameters);

 

We now have a DataReader object containing the returned records, because the DALMSSQL ReadRecords method return a IDataReader interface we need to cast this to a SqlDataReader.

 

Now the interesting bit is, if we decided to change our database to use MySQL instead of MS SQL then the only line we would need to change is

 

DALMSSQL dal = new DALMSSQL

to

DALMYSQL dal = new DALMYSQL

And thats all there is to it

Database’s supported

  • Microsoft SQL Server
  • MySQL

Licence for use

This software is licenced under the GNU General Public License Version 2 which can be read by clicking the link.

Please let me know your comments on the software and feel free to modify and use as you wish. If you do improve the software then please email me back the source code for inclusion in the released version.

  1. May 15, 2007 at 8:42 am | #1

    Thanks mate,

    this should come in handy once i finally get round to building some .net applications!

  2. October 19, 2007 at 8:23 pm | #2

    Have you ever tried Subsonic?

    It’s a great, open source DAL / ORM type thing.

  3. Shane
    December 19, 2007 at 11:12 pm | #3

    interesting, the only thing i would ask is why create instances of the layer, in each application you should only be accessing one database at a time. this is a clue to use a singleton pattern. Why not make a generic IDataLayer object and then set the type when you need it. you can change the type if necessary but you are still creating only 1 instance. i can show you an example that i use if you want.

    I still enjoy your site, well created and nice to look at with good information. good job.

  4. veena khan
    February 15, 2008 at 8:37 pm | #4

    im veena khan
    i wana get help from u in making data access layers in vb.net2003 using sql server 2000
    plz knock knock knock
    anyyyyyyyyy project menagar developer here
    reply me quick on that id
    elite_onlinz@yahoo.com

  5. Jebas
    October 6, 2008 at 10:58 pm | #5

    Hi,

    Could you please show an example?

    Thanks

    Jebas

  1. May 13, 2007 at 1:25 pm | #1