Using the Microsoft Data Access Application Block

Application Blocks are a collection of libraries from Microsoft. Among the list of components will be the Data Access Application Block. This component can help to save considerable time by allowing you to specify several parameters and definitely will meet most if not all of your data access logic needs.

The download is here – http://www.microsoft.com/en-gb/download/details.aspx?id=435. Simply add the DLL to the BIN folder of your application or website. 

The main class for data access is the SqlHelper class. The SqlHelper class simplifies the job of writing data access

ExecuteDataset

A data table can be created with a single line of code. Here’s an example.

Dim dt as System.Data.DataTable = Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteDataset(Conn, System.Data.CommandType.Text, “select * from Products”).Tables(0)

Where Conn is the connection string to the database. Stored procedures can also be called by using CommandType.StoredProcedure. Parameters can be added an array of SqlParameter objects

Dim dt as System.Data.DataTable = Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteDataset(Conn(), System.Data.CommandType.Text, “select * from Products where Field1=@Field1”, New System.Data.SqlClient.SqlParameter("@Field1", Field1)).Tables(0)

Methods are also available for DataReaders, XmlReaders, scalar values and non-queries. Here are some examples.

Dim dr As System.Data.SqlClient.SqlDataReader = Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteReader(Conn, CommandType.Text, "select * from Products")
Dim x As Integer = Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteScalar(Conn, CommandType.Text, "select top 1 ProductID from Products")
SqlHelper.ExecuteNonQuery(Conn, CommandType.Text, "select from Products where ID=1")
Updated: March 6, 2015 — 4:56 pm

Leave a Reply

Your email address will not be published. Required fields are marked *

Charon © 2015 Privacy Policy | Terms & Conditions Frontier Theme