Posts Tagged ‘data access layer objects C# entlib

23
May
08

#04 Microsoft’s Enterprise Data Layer

Note: This is a more advanced programming topic

As we migrated to the 2.0+ (3.x) frameworks I have been using Microsoft’s Enterprise Library and in particular the DAAB (Data Access Application Blocks). Here is a look at one method in the Employee Data Access layer. This is a static wrapper method but keeps the code clean.

There are other method calls but I have stripped them out for the moment.

The following object allows me to bind to a Telerik RadGrid (Gridview)  like this:

protected void rgEmployees_NeedDataSource(object source, GridNeedDataSourceEventArgs e) {

rgEmployees.DataSource = Employee.getAllEmployees();
}

Note: This is an Telerik AJAX method call but the binding to a simple Gridview is just the same. Ideally this receives some search parms that filter the return a tailored dataset.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.Common;
using System.Web.Configuration;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Telerik.WebControls;
using System.Data.SqlTypes;

public class Employee
{
public Employee ()
{

}

/// <summary>
/// Gets All Employees
/// </summary>
/// <returns> dataset of EM data </returns>
static public DataSet getAllEmployees()
{
DataSet ds = null;
DbCommand dbc = null;
Database db = DatabaseFactory.CreateDatabase(“*****”);    // readonly ID
dbc = db.GetStoredProcCommand(“SPName”);                    // SP reference

try
{
ds = db.ExecuteDataSet(dbc);
}
catch (Exception exc)
{
throw new IntranetException(exc.Message,
exc,
SystemConstants.Intranet_ErrorMessage);
}

return ds;

}

}