Now that you have installed Enterprise Library 4.0 and the source you are ready to play with EntLib to get to know the application blocks and corresponding configuration.
Let's start by creating a console application: Start Visual Studio 2008, click File | New Project, select C# console application, enter a name and click OK.
Your first step is to add references to the EntLib dlls. Right-click References, select Add Reference and select the following files:
- Enterprise Library Data Access Application Block (Microsoft.Practices.EnterpriseLibrary.Data.dll)
- Enterprise Library Exception Handling Application Block (Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.dll)
- Enterprise Library Logging Application Block (Microsoft.Practices.EnterpriseLibrary.Logging.dll)
- Enterprise Library Shared Library (Microsoft.Practices.EnterpriseLibrary.Common.dll)
Add the following using statements to the top of your program.cs.
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using Microsoft.Practices.EnterpriseLibrary.Logging;
First we are going to get data from a database. To do this, we need a database to get data from. In this scenario I am using the Northwind sample database. We like to perform data access using stored procedures and for that I created the following procedure:
USE [Northwind]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[SelectCustomers]
AS
SELECT Customers.CustomerID, Customers.CompanyName, Customers.City
FROM Customers
Next go back to your program.cs and in your main method enter:
Database database = DatabaseFactory.CreateDatabase("Northwind");
DataSet ds = database.ExecuteDataSet("SelectCustomers");
What you just did is create a new database object through a database factory and a create database method that takes the configured connectionstring as parameter.
To actually see the data, you can add the following:
int i = ds.Tables[0].Rows.Count;
foreach (DataRow dr in ds.Tables[0].Rows)
{
Console.WriteLine(dr[1].ToString() + " - " + dr[2].ToString());
}
Console.ReadLine();
All you have to do now is create a configure Enterprise Library so that it knows where to find the Northwind database and how to access it. Add an app.config file to your solution and open it either by double clicking it or by opening it in the EntLib Configuration Editor.
If you only see an Enterprise Library Configuration node, right-click it and select New Application. On the application right-click and select New | Data Access Application Block. Expand the Data Access Application Block node, right-click the Connection Strings node and select New Connection String. Rename the new connection string to Northwind, which you used in code, and enter the following ConnectionString: Database=Northwind;Server=(local)\SQLEXPRESS;Integrated Security=SSPI. This string assumes that your database is called Northwind, you are using a local version of SQL Express and you connect to it using integrated security. Accept the default ProviderName and save the configuration.
Now in Visual Studio you can press F5 to run the application and see the result.
To summarize you have created a console application that references Enterprise Library, uses configurable settings to connect to the database and performs data access in just 2 lines of code.
Next we will add code to handle exceptions and to log them.