Developing a Data Access Layer for Sybase using ADO.NET: Essentials Continued - Getting a row of information from Sybase database using ADO.NET
(Page 4 of 4 )
This mainly accepts a SQL SELECT statement as a parameter and simply returns only the first row of the output of the SELECT statement in the form of a “datarow” in ADO.NET. Let us walk through the complete code first:
Public Function getDataRow(ByVal sqlSELECT As String) As
System.Data.DataRow
Dim da As AseDataAdapter
Try
Dim dt As New DataTable
da = New AseDataAdapter(sqlSELECT, _ConnectionString)
da.Fill(dt)
da.Dispose()
If dt.Rows.Count = 0 Then Return Nothing Else Return
dt.Rows(0) 'return only first row
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 object I used is “AseDataAdapter” (to handle the communication between database and offline data). I declare the object as follows:
Dim da As AseDataAdapter
Let us consider the following statement:
da = New AseDataAdapter(sqlSELECT, _ConnectionString)
The statement creates an adapter which tries to execute our SELECT statement (passed in the form of a parameter) based on the database connection created internally. We need not specify any connection related object. Just providing the connection string would also do the job.
Dim dt As New DataTable
da.Fill(dt)
da.Dispose()
If dt.Rows.Count = 0 Then Return Nothing Else Return
dt.Rows(0) 'return only first row
I created a new “datatable” object which tries to hold all the data retrieved by the data adapter. The second statement makes the adapter fill the information into the data table. The last statement in the above code fragment checks to see if any rows exist within the data table. If no rows are available, the method returns nothing or else returns only the first row (which is nothing but the data row).
I shall cover the rest of the methods in upcoming articles, so make sure that you sign up for a newsletter to get notified. Any comments, suggestions, feedback, bugs, errors, enhancements are highly appreciated at jag_chat@yahoo.com
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |