Back to previous

Use Enterprise Library for Data Access in one line code

Enterprise Library is a very useful component developed by Microsoft to help developers solving many common tasks. One that I use a lot is the data access block, which allows you to run data access query in just one line of code! (Saves all the work in writing a command and pass to a reader, etc).

To use Enterprise Library, all have to do is:

1. Download the component and install.

2. Add reference to your project (Microsoft.Practices.EnterpriseLibrary.Data and Microsoft.Practices.EnterpriseLibrary.Common).

3. Configure your web.config:

//under configsections
Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data

Set a default database:

dataConfiguration defaultDatabase="AdventureWorksDB"

Now we can query data access in just one line of code:

DataTable dtt = DatabaseFactory.CreateDatabase().ExecuteDataSet("StoredProcName").Tables[0];

ShareThis

If you think this post is useful, please recommend me at the bottom of the page. ;)

Discussion