Developing a Data Access Layer for Sybase Using ADO.NET: Essentials - Getting a table (or data table) of information from Sybase database using ADO.NET
(Page 4 of 5 )
This is one of the most frequently used methods (“getDataTable”) in the DAL. This mainly accepts an SQL SELECT statement as a parameter and simply returns the output of the SELECT statement in the form of a “data table” in ADO.NET. Let us walk through it first:
Public Function getDataTable(ByVal sqlSELECT As String) As
System.Data.DataTable
Dim Conn As AseConnection
Dim da As AseDataAdapter
Try
Conn = New AseConnection(_ConnectionString)
Dim dt As New DataTable
da = New AseDataAdapter(sqlSELECT, Conn)
da.Fill(dt)
da.Dispose()
Return dt
Catch ex As Exception
Try
da.Dispose()
Catch e As Exception
'do nothing...if still error persists
End Try
Throw New Exception(ex.Message & ". SQL Statement: "
& sqlSELECT)
End Try
End Function
The main native Sybase objects I used are “AseConnection” (to work with the database connection) and “AseDataAdapter” (to handle the communication between the database and offline data). I declare both of those objects as follows:
Dim Conn As AseConnection
Dim da As AseDataAdapter
Let us consider the following two statements:
Conn = New AseConnection(_ConnectionString)
da = New AseDataAdapter(sqlSELECT, Conn)
The first statement creates a new Sybase database connection (natively) using the connection string defined in the web.config file. The second statement creates an adapter, which tries to execute our SELECT statement (passed in the form of a parameter) based on the previous database connection.
Dim dt As New DataTable
da.Fill(dt)
da.Dispose()
Return dt
I created a new “datatable” object, which tries to hold all the data retrieved by the data adapter. The second statement (in the fragment above) executes our SELECT statement and the output (or result) is filled into the data table, which is finally returned from the function.
Next: Getting a single value from Sybase database using ADO.NET >>
More ASP.NET Articles
More By Jagadish Chaterjee