Developing an Object Oriented Business Component Using WCF and Visual Studio 2008 - Understanding the WCF Business Component: Source Code for “DBHelper”
(Page 5 of 5 )
I kept this utility class (“DBHelper”) very simple. It can be enhanced by implementing features like stored procedure access, support for transactions and so on.
In fact, I suggest you go to Microsoft Enterprise Library and use the Data Access Application block as part of the “DBHelper” façade. If you are new to Microsoft Enterprise Library (or Data Access Application block), you can find my excellent series on the subject at:
http://www.aspfree.com/c/a/ASP.NET/Developing-ASPNET-20-Applications-with-
the-Microsoft-Data-Access-Application-Block/
http://www.aspfree.com/c/a/ASP.NET/Interacting-with-Databases-Using-ASPNET-
20-with-the-Microsoft-Data-Access-Application-Block/
http://www.aspfree.com/c/a/ASP.NET/Working-with-Stored-Procedures-using-
ASPNET-20-with-Microsoft-Data-Access-Application-Block/
http://www.aspfree.com/c/a/ASP.NET/More-about-Stored-Procedures-using-
ASPNET-20-with-the-Microsoft-Data-Access-Application-Block/
The following is the code used for simple data access:
Imports System.Data
Imports System.Data.SqlClient
Public Class DBHelper
Private Shared Function getConnectionString() As String
Return My.Settings.cnNorthwind
End Function
Public Shared Sub SQLExecute(ByVal strSQL As String)
Dim Conn As New SqlConnection(getConnectionString)
Dim cmd As New SqlCommand(strSQL, Conn)
With cmd
.Connection.Open()
.ExecuteNonQuery()
.Connection.Close()
.Dispose()
End With
End Sub
Public Shared Function getRow(ByVal strSELECT As String) As DataRow
Dim da As New SqlDataAdapter(strSELECT, getConnectionString)
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
End Function
Public Shared Function getDataTable(ByVal strSELECT As String) As DataTable
Dim Conn As New SqlConnection(getConnectionString)
Dim da As New SqlDataAdapter(strSELECT, Conn)
Dim dt As New DataTable
da.Fill(dt)
da.Dispose()
Return dt
End Function
Public Shared Function getRowValue(ByVal strSELECT As String) As String
Dim Conn As New SqlConnection(getConnectionString)
Dim cmd As New SqlCommand(strSELECT, Conn)
Dim value As String = ""
With cmd
.Connection.Open()
value = .ExecuteScalar() & "" 'concatenating an empty string..to eliminate null or nothing
.Connection.Close()
.Dispose()
End With
Return value
End Function
End Class
Hosting, managing and accessing (or consuming) the WCF Service are very similar to my previous articles and I suggest you refer to those.
I hope you enjoyed the article and any suggestions, 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. |