Click here to Skip to main content
15,170,331 members
Articles / Programming Languages / C#
Posted 5 Aug 2009

Tagged as


28 bookmarked

Using Microsoft Enterprise Library Data Access Application Block – Part II

Rate me:
Please Sign up or sign in to vote.
3.07/5 (16 votes)
7 Aug 2009CPOL3 min read
Using Microsoft Enterprise Library Data Access Application Block to Retrieve Data from Database.


In my previous article, Using Microsoft Enterprise Library Data Access Application Block – Part I, I have shown how to use the Data Access Application Block to insert and update data in a database using a Stored Procedure, following a layered architecture. In this article, let me show how to use the Data Access Block to retrieve data.

Please note that I have used the Microsoft Enterprise Library 4.1 – October, 2008 release. It works on .NET framework 3.5 and Visual Studio 2008.

You can download Microsoft Enterprise Library 4.1 from this URL:

Integration and Usage

  • Code: In the attached source, I have put in the Data Access Layer classes, the Entity class, the EventLog class, and the SQL script to create the table and relevant Stored Procedures. Also the relevant assemblies from the Data Access Application block. The code is to get user information (User Details based on UserID and All Active Users) from the database. I have put in the classes in the same folder, but they are in different namespaces and assemblies. It is a better practice to put in different namespaces for different layers and in different assemblies.
  • Explanation: First, we need to install Microsoft Enterprise Library 4.1 and get hold of the assemblies “Microsoft.Practices.EnterpriseLibrary.Common.dll” and “Microsoft.Practices.EnterpriseLibrary.Data.dll” and add references to them in your project. In the attached sample, you will find the necessary DLLs in case you don’t want to download and install Microsoft Enterprise Library 4.1.
  • User.cs: The Entity Layer class. This class is used to pass user data between various layers.
  • UserDAC.cs: The Data Access Layer class. This class has methods to get User Details by ID and get All Active Users. The methods execute the relevant procedures to get the user info. This class should be called from the Business Layer class to get data from the database. The Business Layer will call the relevant methods of the Data Layer class, which in turn responds back with the User Entity object for User Details and a generic list for All Active Users.
  • To note, this class inherits the DataAccessComponent class. The DataAccessComponent has methods to get data from the result-sets returned by the procedure in a generic fashion. The ParseUser method of the UserDAC class is used to populate the User entity object by iterating through the DataReader. The GetUserDetailsById returns the User entity object and also the message ID and message string. In case the user is not existent for the user ID that is passed to this method, the message ID would return a negative integer and message string would return an error string. I have kept this simple for the demo purpose; however, this is configurable as per the application needs and architecture.

    The User Data Access class will also log the error in the Application Event Log using the EventLog class.

    public User GetUserDetailsById(int vintUserId, 
           out int vintMessageId, out string vstrMessage)
        Database objDB = new SqlDatabase(ConnectionString);
        using (DbCommand objCMD = 
            objDB.AddInParameter(objCMD, "@intUserID", DbType.Int32, vintUserId);
            objDB.AddOutParameter(objCMD, "@intErrID", 
                                  DbType.Int32, Int32.MaxValue);
            objDB.AddOutParameter(objCMD, "@strMessage", DbType.String, 255);
            User objUser = null;
            using (IDataReader objDataReader = objDB.ExecuteReader(objCMD))
                if (objDataReader.Read())
                    objUser = ParseUser(objDataReader);
                    vintMessageId = vintUserId;
                    vstrMessage = null;
                    vintMessageId = Convert.ToInt32(objDB.GetParameterValue(objCMD, 
                    vstrMessage = objDB.GetParameterValue(objCMD, 
            catch (Exception ex)
                EventLog objLog = new EventLog();
                throw ex;
            return objUser;
    public List<user> GetAllActiveUsers()
        Database objDB = new SqlDatabase(ConnectionString);
        using (DbCommand objCMD = 
            List<user> objUserList = new List<user>();
                using (IDataReader objDataReader = objDB.ExecuteReader(objCMD))
                    while (objDataReader.Read())
                        User objUser = ParseUser(objDataReader);
            catch (Exception ex)
                EventLog objLog = new EventLog();
                throw ex;
            return objUserList;
    private User ParseUser(IDataReader vobjDataReader)
        User objUser = new User();
        objUser.UserID = base.GetDataValue<int>(vobjDataReader, "UserID");
        objUser.UserName = base.GetDataValue<string>(vobjDataReader, "UserName");
        objUser.FirstName = base.GetDataValue<string>(vobjDataReader, "FirstName");
        objUser.LastName = base.GetDataValue<string>(vobjDataReader, "LastName");
        objUser.UserPassword = base.GetDataValue<string>(vobjDataReader, "UserPassword");
        return objUser;

    The above methods are from the UserDAC class to get the User Info from the database and return the user entity object or a generic List of User Entity objects to the caller.

  • EventLog.cs: The Framework / Utility layer class. This class is used to log error information into the Application Event Log of the system. Refer my article Using Microsoft Enterprise Library Logging Application Block for details on event logging mechanisms using the Microsoft Enterprise Library Logging Application Block.
  • SQLScripts.sql: The SQL scripts to create the database table and the Stored Procedures used in the example.


By now, you can understand how simple it is to use the Enterprise Library 4.1 application blocks for Data Access to retrieve data from a database. To study more on the Microsoft Enterprise Library 4.1 application blocks, refer the URL


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Comments and Discussions

Questionusing this [DataMember] with [DataMapping("idEmployee")] Pin
alsholi20-Apr-12 6:00
Memberalsholi20-Apr-12 6:00 
GeneralMy vote of 1 Pin
eferreyra21-May-10 9:14
Membereferreyra21-May-10 9:14 
Questionhow can get query result and output parameter togther ? Pin
YawerIqbal9-Jan-10 17:37
MemberYawerIqbal9-Jan-10 17:37 

thanks for such a nice article. Please guide me how I can receive query result (a table) and output parameter from stored procedure both at a time.

One more thing can we use different databases like say Oracle or mysql with it?


GeneralNice Example and Code Pin
Aditithegreat4-Sep-09 18:40
MemberAditithegreat4-Sep-09 18:40 
GeneralRe: Nice Example and Code Pin
Robin_Roy6-Sep-09 19:17
MemberRobin_Roy6-Sep-09 19:17 
GeneralGood Article Pin
rahulthecoder2-Sep-09 19:35
Memberrahulthecoder2-Sep-09 19:35 
GeneralRe: Good Article Pin
Robin_Roy6-Sep-09 19:16
MemberRobin_Roy6-Sep-09 19:16 
GeneralExcellent Pin
SaswatiS26-Aug-09 18:43
MemberSaswatiS26-Aug-09 18:43 
GeneralRe: Excellent Pin
Robin_Roy26-Aug-09 23:35
MemberRobin_Roy26-Aug-09 23:35 
GeneralExcellent Article Pin
arvindjo26-Aug-09 0:18
Memberarvindjo26-Aug-09 0:18 
GeneralRe: Excellent Article Pin
Robin_Roy26-Aug-09 23:36
MemberRobin_Roy26-Aug-09 23:36 
Generalgood one Pin
lxjhonson24-Aug-09 23:56
Memberlxjhonson24-Aug-09 23:56 
GeneralRe: good one Pin
Robin_Roy26-Aug-09 23:37
MemberRobin_Roy26-Aug-09 23:37 
QuestionPart II? Pin
Stanislav Georgiev19-Aug-09 21:29
MemberStanislav Georgiev19-Aug-09 21:29 
GeneralPoor article ! Pin
kalyankrishna110-Aug-09 22:10
Memberkalyankrishna110-Aug-09 22:10 
GeneralMy vote of 1 Pin
kalyankrishna110-Aug-09 22:09
Memberkalyankrishna110-Aug-09 22:09 
GeneralGood Article Pin
arvindcoolest10-Aug-09 18:18
Memberarvindcoolest10-Aug-09 18:18 
GeneralRe: Good Article Pin
Robin_Roy26-Aug-09 23:40
MemberRobin_Roy26-Aug-09 23:40 
GeneralMy Vote of 5 Pin
johnclark6410-Aug-09 18:11
Memberjohnclark6410-Aug-09 18:11 
GeneralRe: My Vote of 5 Pin
Robin_Roy26-Aug-09 23:41
MemberRobin_Roy26-Aug-09 23:41 
GeneralMy Vote of 5 Pin
brown2010-Aug-09 17:39
Memberbrown2010-Aug-09 17:39 
GeneralRe: My Vote of 5 Pin
Robin_Roy26-Aug-09 23:42
MemberRobin_Roy26-Aug-09 23:42 
GeneralMy Vote of 5 Pin
albert_cook10-Aug-09 17:36
Memberalbert_cook10-Aug-09 17:36 
GeneralRe: My Vote of 5 Pin
Robin_Roy26-Aug-09 23:44
MemberRobin_Roy26-Aug-09 23:44 
GeneralMy vote of 1 Pin
Tawani Anyangwe10-Aug-09 3:38
MemberTawani Anyangwe10-Aug-09 3:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.