Interacting with Databases Using ASP.NET 2.0 with the Microsoft Data Access Application Block - Retrieving a table of rows in the form of a data table using the Data Access Application Block
(Page 2 of 6 )
In previous section, we worked with a dataset. In this section, we shall retrieve a data table by passing a SELECT statement to the Data Access Application Block.
The following is the complete code needed to retrieve a data table by passing a SELECT statement to the Data Access Application Block.
ImportsSystem.Data
ImportsMicrosoft.Practices.EnterpriseLibrary.Data.Sql
ImportsMicrosoft.Practices.EnterpriseLibrary.Data
PartialClass _Default
Inherits System.Web.UI.Page
Protected Sub btnConnect_Click(ByVal sender As Object, ByVal
e As System.EventArgs) Handles btnConnect.Click
Me.GridView1.DataSource = getDataTable("select * from
HumanResources.department")
Me.GridView1.DataBind()
End Sub
Private Function getDataTable(ByVal SQL As String) As
DataTable
Dim db As SqlDatabase = DirectCast(DatabaseFactory.CreateDatabase("AdventureWorks"), SqlDatabase)
Dim dt As DataTable = db.ExecuteDataSet(CommandType.Text,
SQL).Tables(0)
Return dt
End Function
EndClass
You can observe that in the above code, I defined my own function, “getDatatable,” which accepts a SELECT statement as a parameter and returns a “Datatable” object. This lets us reuse the function any number of times within the same form very easily.
You can also observe that I am returning only the data table from within the function “getDataTable,” by selecting only the first table in the dataset as follows:
Dim dt As DataTable = db.ExecuteDataSet(CommandType.Text,
"select * from HumanResources.department").Tables(0)
Return dt
Next: Retrieving a single row in the form of a data row using the Data Access Application Block >>
More ASP.NET Articles
More By Jagadish Chaterjee