The Data Access Application Block encapsulates performance and resource management best practices. It encapsulates two common patterns encountered in application programming, data access and exception management. Application Blocks provide value by making our programming more efficient and our programs more maintainable. and it can easily be used as a building block in your own .NET application. They speed the application development process by "wrapping" common tasks. If you use it, you will reduce the amount of custom code you need to create, test and maintain.
Specifically, the Data Access Application Block will help you:
- Call stored procedures or SQL text commands
- Specify parameter details
- Return SqlDataReader, DataSet or XmlReader object, or single values.
Important Information:
A newer version of this application block has been released as a part of the patterns & practices Enterprise Library. Don't worry, the application block you have requested is still available for download but is now considered as an archive release.First of all we will consider how install Application Blocks on your system.
- Download the Application Block installer package and install the Application Block (the URL for the Data Access Application Block is http://download.microsoft.com
/download/VisualStudioNET ),/daabref/RTM/NT5/EN-US/DataAcce ssApplicationBlock.msi -
Navigate to the "Microsoft Application Blocks for .NET" item from Start Menu -> Programs Files and select the language of your choice (C# or VB).
-
The Application Block Project will open in Visual Studio. Build the project. (If you use newer version of visual studio you may have to convert the project. Don't worry, It will work)
- Once the assembly has been built, add a reference to Microsoft.ApplicationBlocks
.Data.dll to your project and add using (C#) or Imports (VB) statements for the Microsoft.ApplicationBlocks.Data
When you work with data base you may have to use following namespace also. System.Data, and System.Data.SqlClient.
Now you are ready to use DAAB
In order to illustrate the one of advantage of using the Data Access Block, now lets quick look at following examples,
Data read through SqlDataReader and bind to a DataGrid. We need to use the SqlHelper class's static ExecuteReader() method only.
//create the connection string and sql statment to be executed
string strSql = "select * from products where categoryid = 1";
string strConnTxt = "Server=(local);Database=Northwind;Integrated Security=True;"; DataGrid1.DataSource = SqlHelper.ExecuteReader(strConnTxt,CommandType.Text,strSql);DataGrid1.DataBind();
You can see there is considerably less code need to do this compare with same task doing without using DAAB. Consider following code:
//create the connection string and sql to be executed string strConnTxt = "Server=(local);Database=Northwind;Integrated Security=True;";
string strSql = "select * from products where categoryid = 1"; //create and open the connection objectSqlConnection objConn = new SqlConnection(strConnTxt);
objConn.Open();
//Create the command objectSqlCommand objCmd = new SqlCommand(strSql, objConn);
objCmd.CommandType = CommandType.Text; //databind the datagrid by calling the ExecuteReader() method
DataGrid1.DataSource = objCmd.ExecuteReader();DataGrid1.DataBind();
//close the connectionobjConn.Close();The main advantage of the Application Blocks is that they greatly reduce the amount of code you need to write. Writing less code means more than just shorter time needed to write the code, It also means fewer bugs, and an overall lower total cost to produce the software.
Using the Data Access Application Block to Execute Stored Procedures
The ExecuteReader() method also has several overloads that enable you to perform stored procedures and transactions. Let's take a quick look at the same method, but this time we'll execute a stored procedure: You need to send the name of the stored procedure and a SqlParameter objects as parameter of stored procedure.
DataGrid2.DataSource = SqlHelper.ExecuteReader(strConnTxt,CommandType.StoredProcedure, "getProductsByCategory", new SqlParameter("@CategoryID", 1));
DataGrid2.DataBind();
To be continued