Microsoft Enterprise Library for Database

What is EnterPrise Library?

Microsoft Enterprise Library is a collection of reusable application blocks designed to assist software developers with common enterprise development challenges. It includes:

  • Data Access Application Block
  • Cryptography Application Block
  • Caching Application Block
  • Exception Handling Application Block
  • Logging Application Block
  • Policy Injection Application Block
  • Security Application Block
  • Validation Application Block
  • Unity Application Block

Enterprise Library for Data Access Application Block

Scope of this article is to describe how a user can use enterprise library for data access layer.

Prerequisite required:
1.Basic knowledge of ASP.NET
2.Basic knowledge of ADO.NET
3.Basic knowledge of database

Steps to follow

Download Enterprise Library from Microsoft website and install it. After installation you will get a location where multiple “dll” files will be available. We just need to get reference of these “dll”s in order to use them in our application. After installation you will see below “dll”s are available for reference –

  • Microsoft.ApplicationBlocks.Data.dll
  • Microsoft.Practices.EnterpriseLibrary.Common.dll
  • Microsoft.Practices.EnterpriseLibrary.Data.dll
  • Microsoft.Practices.ObjectBuilder2.dll
  • Microsoft.Practices.Unity.dll

Among these libraries, we only need 2 references to get database object with configuration connection string, these are –

  • Microsoft.Practices.EnterpriseLibrary.Common.dll
  • Microsoft.Practices.EnterpriseLibrary.Data.dll

Secondly,

  • Create an ASP.NET application
  • Add configuration section in web.config
  • <configSections>
    <section name=”dataConfiguration” type=”Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35″/>
    </configSections>

  • Add connection string section in web.config
  • <connectionStrings>
    <add name=”ConnectionStringName” connectionString=”Data Source=.;Initial Catalog=Northwind;User ID=sa;Password=password” providerName=”System.Data.SqlClient”/>
    </connectionStrings>

  • Add data configuration for default database in web.config
  • <dataConfiguration defaultDatabase=”ConnectionStringName”/>

This all are configuration setting to use Enterprise Library for database connection. Now we will see how to create a database object with this connection string.

Creating Database Objects

  • Add reference to your page where you want to create database object.
    1. using Microsoft.Practices.EnterpriseLibrary.Data;
    2. using System.Data.Common;
  • Add these lines of code to get database object
    1. Database objDb = DatabaseFactory.CreateDatabase(); OR
    2. Database objDb = DatabaseFactory.CreateDatabase(“ConnectionStringName”);

Constructor without parameter will get default database connection string whereas with parameter will search respective connection string name and created database object over given connection string.

Code To get Data from Database

Although you are done with database object, still we will move ahead with some sample code to get data from database.

DataSet dsSalesReports = new DataSet();
DbCommand dbCommand = objDb.GetStoredProcCommand(“Employee Sales by Country”);

// Adding stored procedure parameters

objDb.AddInParameter(dbCommand, “@Beginning_Date”, DbType.Date, DateTime.Now.AddYears(-25));
objDb.AddInParameter(dbCommand, “@Ending_Date”, DbType.Date, DateTime.Now);

// Loading dataset based on parameters
objDb.LoadDataSet(dbCommand, dsSalesReports, “SalesReports”);

// Binding gridview with resultset
GridViewSalesReport.DataSource = dsSalesReports;
GridViewSalesReport.DataBind();

That’s all for database connection with Enterprise Library.

Advertisements

About Gokul Dahal, Nepal

Software Engineer, Freelancer, Design and Develop web applications,Software Developer Nepal, FreeLancer Nepal Contact : gokuldahal@gmail.com
This entry was posted in ASP.NET, SQL and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s