Database Independent Development using ASP.NET 2.0 - Developing a simple data access class to work with the factory: methods extended
(Page 6 of 6 )
The following is the method which retrieves a single row in the form of a data row:
Public Function GetDataRow(ByVal sqlSELECT As String) As DataRow
Dim dt As DataTable = GetDataTable(sqlSELECT)
If dt.Rows.Count > 0 Then
Return dt.Rows(0)
Else
Return Nothing
End If
End Function
The above function works with the previous GetDataTable function to return a set of rows. The following is the method which retrieves a single value in the form of a string from the database:
Public Function GetValue(ByVal sqlSELECT As String) As String
Dim cmd As DbCommand = GetDBCommand()
cmd.CommandText = sqlSELECT
cmd.CommandType = CommandType.Text
Try
cmd.Connection.Open()
Return cmd.ExecuteScalar & ""
Catch ex As Exception
Throw New Exception(ex.Message & "-->" & sqlSELECT)
Finally
If cmd.Connection.State = ConnectionState.Open Then
cmd.Connection.Close()
End If
cmd.Dispose()
End Try
End Function
You can further enhance the above classes with the support of transactions, store procedures etc. You can find an ASP.NET application for testing along with the above classes as part of the free download.
I hope you enjoyed the article and any comments, suggestions, feedback, bugs, errors, enhancements etc. are highly appreciated at http://jagchat.spaces.live.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. |