1. Implementing the data Access layer
This section describes the implementation of the data access layer, which includes all the necessary classes and methods for communicating with the AdventureWorks database. First, create a new Visual C # class Library project adventureworkstraderdataaccess using Visual Studio 2005. When this project is created, you can modify the default class name to Productcategorydb. Example 1 illustrates the implementation code for the Productcategorydb class.
Example 1: Implementing the Productcategorydb Class
Using System;
Using System.Data;
Using System.Data.Common;
Using System.Data.SqlClient;
Using System.Collections.Generic;
Using System.Text;
Using Adventureworkstraderentities;
Using Microsoft.Practices.EnterpriseLibrary.Data; Namespace Adventureworkstraderdataaccess {public class Productcategorydb {private datacolumnmapping[] mappings = new Dat Acolumnmapping[] {new DataColumnMapping ("ProductCategoryID", "ProductCategoryID"), new DataColumnMapping ("Name", "
Name "), new DataColumnMapping (" Rowguid "," rowguid "), new DataColumnMapping (" ModifiedDate "," ModifiedDate ")}; Public ilist〈productcategory> getproductcategories () {ilist〈productcategory> list = new List〈productcategory
> ();
Database db = Databasefactory.createdatabase ();
String storedprocedurename = "Getproductcategories"; DbCommand DbCommand = db.
Getstoredproccommand (StoredProcedureName); using (IDataReader reader = db. ExecuteReader (DbCommand)) {while reader.
Read ()) {ProductCategory temp = new ProductCategory (); ProdUctcategory category = (productcategory) dataaccesshelper.populateentity (temp, mappings, reader); List.
ADD (category);
} return list; }
}
}