Download Sample Application : EntLib_DataAccess.rar (657.66 kb)
In previous post "Microsoft Enterprise Library 6 - new release" we have already discussed about the new release of Microsoft Enterprise Library 6. In this post, we will see how easy it is to use Data Access Application Block (DAAB) in .net applications. It is practical oriented post and cover steps to implement rather than explaining theory.
In the sample solution, we will add following projects-
- Class Library - for Data Transfer Objects
- Class Library - for Data Access
- Website - for User Interaction
Well, we will go one by one with following steps -
1. Downloaded the full version exe file that contains all the Enterprise Library binaries and scripts. When you will run exe, it will ask to choose location where you want to keep these files. Here, select a folder where you want to keep the extracted files.
2. Since, we are going to use a separate class library project for data access, we will require to reference the required assemblies into it.
So, we will include following assemblies into our class library project i.e. CP.DataAccess-
i. Microsoft.Practices.EnterpriseLibrary.Data and
ii. Microsoft.Practices.EnterpriseLibrary.Common
3. Next job is to configure the settings for source configuration and connection string. We can configure our settings in 2 ways-
√ File based configuration source (XML file in standard .net configuration format)
√ System configuration source ( app.config or web.config files)
In our sample solution, we are going to configure connection string only to avoid complexity. Below image is the sample that shows how source configuration and connection string setting is done.
4. Now, it’s all ready to use.
//1. We will import namespace as –
//************************************************
using Microsoft.Practices.EnterpriseLibrary.Data;
using CP.DataTransferObject; //this is for our DTO library proj
//using Microsoft.Practices.EnterpriseLibrary.Common;
//2. Create an instance of Database class using the Database Factory
//************************************************
// if we have configured web.config with defaultDatabase then no need to give connection string
// Database db = DatabaseFactory.CreateDatabase();
Database db = DatabaseFactory.CreateDatabase("ConStringAutoMilesSqlDB");
//3. Create connection
//************************************************
DbConnection conn = db.CreateConnection()
conn.Open();
//4. Create command and Execute Reader to get the result data
//************************************************
DbCommand cmd = conn.CreateCommand()
//Set command type, in our sample we will use stored procedure
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "uspGetModelsByCode";
cmd.Parameters.Add(new SqlParameter("@modelCode", modelCode) { SqlDbType = SqlDbType.VarChar });
SqlDataReader dr = cmd.ExecuteReader() as SqlDataReader
The complete data access code would be as -
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using CP.DataTransferObject;
//using Microsoft.Practices.EnterpriseLibrary.Common;
namespace CP.DataAccess
{
publicclassModelMasterDataAccess
{
// POINT-(1) : Set SetDatabaseProviderFactory
static ModelMasterDataAccess()
{
DatabaseFactory.SetDatabaseProviderFactory(newDatabaseProviderFactory());
}
publicIList<ModelsDTO> GetModelList(string modelCode)
{
//These 2 lines are used if POINT-(1) approach is not used
// DatabaseProviderFactory factory = new DatabaseProviderFactory();
// Database db = factory.Create("ConStringAutoMilesSqlDB");
Database db = DatabaseFactory.CreateDatabase("ConStringAutoMilesSqlDB");
IList<ModelsDTO> ModelMasters = newList<ModelsDTO>();
using (DbConnection conn = db.CreateConnection())
{
conn.Open();
using (DbCommand cmd = conn.CreateCommand())
{
//Set command type, in our sample we will use stored procedure
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "uspGetModelsByCode";
cmd.Parameters.Add(newSqlParameter("@modelCode", modelCode) {
SqlDbType = SqlDbType.VarChar });
//Execute Reader to get the result data
using (SqlDataReader dr = cmd.ExecuteReader() asSqlDataReader)
{
while (dr.Read())
{
ModelMasters.Add(newModelsDTO
{
Name = dr.GetString(1),
VendorName = dr.GetString(2),
Code = dr.GetString(3),
LaunchedOn = dr.GetDateTime(4)
});
}
}
}
}
return ModelMasters;
}
}
}
Note: New Enterprise Library version 6 requires to set for factory method if we are using xml configuration files to configure application blocks. Since we are using Data Access Application Block, we need to set DatabaseProviderFactory. Refer previous post: Database provider factory not set for the static DatabaseFactory
Also, if application doesn’t have so many data source configurations and connection strings, you can bypass web.config file configuration though it is highly not recommended. You can just see in attached sample solution, where web.config file doesn’t has any setting even it uses Data Access Application Block. It is because, we have referenced required assemblies into our project and it makes us able to use.
@AnilAwadh