Who is online? 72 guests and 0 members
Member login | Become a member
home » articles » Creating a Simple Data Access Framework in ASP.NET
4/16/2009 by Vinz
Introduction: This article describes on how to create a data access architecture using sqlclient objects with ADO.NET that will returns a DataTable. It also discusses here on how to use and access a certain method in a particular class for you to manipulate the data into your codes. Please note that I am using the Northwind database here and all codes in this article is written in C# language.STEP1: Creating a Class
First, I added some folders under my App_Code folder to store some classes. These class includes the following below:• CommonQueries.cs - is class that contains all the sql string queries which can be reference in the DAL.cs class.• DAL.cs - is class that executes the request from the user. Its basically executes the Insert, Update, Delete and Fetching of data from the database.• DBConnection.cs - is a class that contains the connection string
See the figure below.
STEP 2: Setting up the Connection string
In this article I am setting the connection string under the ConnectionString tag in the web config file like below.
Note that the attribute “key” will serves as the identifier for the connection string.STEP 3: Calling the connection string in the class
After setting up the connection string in the web config, the next step is to call that connection string in the DBConnection.cs class that I have been added earlier in the App_Code folder. This is to achieve reusability in accessing the connection string and to reduce writing codes in our application.
The DBConnection Class
Note: MyDBConnection is the name of the connectionstring that was set up in the webconfig.
STEP 4: Creating the DAL.cs Class
Data Access Layer (DAL) is basically a class that contains the methods for Insert,Update,Delete and Fetch execution. This will help developers minimize their efforts for manipulating data back and forth because the DAL class in intended to obtain a reusable codes. The DAL Class
using System.Data.SqlClient;
The following are the names of the DAL methods with its corresponding descriptions.
Note that you should declare the namespace below in your class fro you to use SqlClient libraries.
STEP 5: Creating the CommonQueries.cs Class CommonQueries Class basically contains all the SELECT, UPDATE, INSERT and DELETE sql string queries. What I mean is that all the queries are set up in this class. You can write or add any methods this class based on your requirements. Basically CommonQueries class calls the DAL class with or without the paramaters. The DAL class will then executes the commands being requested and returns it to the Methods under CommonQueries that calls for it. In this article, I will just show to you on how are we going to add a simple methods within the CommonQueries class. I also include a methods here for Fetching,Inserting and deleting data to the database. Take a look at the following code blocks belowThe CommonQueries Class
The following are the names of the CommonQueries method with its correspondin descriptions.
STEP 6: Accessing the Class in the Page After creating those classes then we can now test for it. In this article I will be calling the GetAllCustomer() and GetCustomerDetails() methods in the CommonQueries and populate my DropDownList and GridView. Basically the DropDownList will be populated by the CustomersID and the GridView will be populated based on the selected ID from the DropDownList. Here's the sample code blocks below:
The ASPX Source:
OUTPUT: here's the output below for that query.That's it.
Comment (No HTML)
It's fast, easy and free! Submit articles, get your own blog, ask questions & give answers in the forums, and become a better developer, faster.
enter your email address: