Microsoft enterprise data application blocks is an excellent data access layer component which can be used to reduce the coding effort for database applications significantly. Its a free download from MSDN
The goals of Enterprise Library are the following:
- Consistency. All Enterprise Library application blocks feature consistent design patterns and implementation approaches.
- Extensibility. All application blocks include defined extensibility points that allow developers to customize the behavior of the application blocks by adding their own code.
- Ease of use. Enterprise Library offers numerous usability improvements, including a graphical configuration tool, a simpler installation procedure, and clearer and more complete documentation and samples.
- Integration. Enterprise Library application blocks are designed to work well together or individually.
- For .Net framework 3.5 - http://msdn.microsoft.com/en-us/library/dd203099.aspx
- For .Net framework 2.0 - http://msdn.microsoft.com/en-us/library/aa480453.aspx
- For .Net framework 1.1 - http://www.microsoft.com/downloads/details.aspx?FamilyId=A7D2A109-660E-444E-945A-6B32AF1581B3&displaylang=en
- How to set up and use Enterprise library data application block data access layer
- Download and Install the MSI file
- Reference the following DLLs from the bin folder of your installed path (for me it was in C:\Program Files\Microsoft Enterprise Library 3.1 - May 2007\Bin) to your .Net application
- Thats it you are now ready to use the classes and functions available in the Enterprise library data application block DAL
1. General structure
//Add the following Using statements at the top of the Class file
//initalize the Database object using the ConnectionString name
Database db = DatabaseFactory.CreateDatabase(Connection string name from web.config);
//Execute an SQL Query using the Execute methods available
db.ExecuteFunctionName(Stored Procedure Name, comma seperated parameters);
//This is the common structure I use in my projects.
//You can find lot of other ways to execute db queries like
//passing the row Command instead of SPROC name.
2. Insert, Update and Delete
//For insert, update and delete use ExecuteNonQuery
Database db = DatabaseFactory.CreateDatabase("ConString");
db.ExecuteNonQuery("usp_user_insert", username, emailID, password);
3. Returning Single values
//If your SPROC return single value, use ExecuteScalar
object pollID = db.ExecuteScalar("usp_poll_insert", question);
4. Selecting data
//use ExecuteDataSet to select data
DataSet ds = db.ExecuteDataSet("usp_users_select");
Just imagine how many lines of code it can easily reduce to execute a query which have a number of parameters.
From now on I will use this Enterprise library data application block data access layer for all the code samples in this blog.
NOTE: The Microsoft Enterprise Library coryntains lot of other really useful Application blocks such as Caching Application block and Cryptography Application Block.