A Wrapper Class for the SQL Server Based Data Access Helper
(Page 1 of 7 )
This article explains to you the techniques required to develop a wrapper class for the data access helper developed in previous articles.
There are two downloadable zip files available for this article. You can find them
here and
here.
This is an extension to my previous articles; it is not an introductory article to COM+ or ADO.NET. I suggest you to refer my previous articles before continuing with this one. If you are very new to COM+, I suggest you go through one of my previous articles here.
I developed this wrapper class for extensibility. Currently, it works only with SQL Server. But you can add few more COM+ classes and link this wrapper very easily, due to its extensibility.
The entire source code for this article is available in the form of downloadable zip files. The solution was developed using Microsoft Visual Studio 2003 Enterprise Architect with Microsoft SQL Server 2005 Developer Edition on Microsoft Windows Server 2003 Enterprise Edition. Even though I believe that the source code available with this contribution can work with Microsoft Visual Studio.NET 2002, I didn’t really test it in any other environment. I request that you post in the discussion area if you have any problems with execution.
Designing the wrapper for extensibility
Let us consider the following code fragment:
PublicEnum DBConnType
MSSQL
EndEnum
PublicClass DBLib
Friend ObjLib As Object 'can be used flexibly in any of the sub-classes
Private arTransCommands As ArrayList
Private _ConnectionString As String
Private _ConnectionType As DBConnType
Public Sub New(ByVal ConnectionType As DBConnType, ByVal ConnectionString As String)
Try
_ConnectionString = ConnectionString
_ConnectionType = ConnectionType
If Len(Trim(_ConnectionString & "")) = 0 Then Throw New Exception("Connection String not configured")
Catch ex As Exception
Throw New Exception(ex.Message & ". Invalid Database Configuration -->Error Logged")
End Try
End Sub
Currently, I developed the above only for the SQL Server type of database connection. When you require support for more than one database, you will add a few more database types to the enumeration.
The variable “arTransCommands” is mainly used to add all the commands which need to be executed as a single transaction.
The following is the code needed to work with the respective database related object:
FriendFunction getDBHelperObject() As Object 'can be used flexibly in any of the sub-classes
Select Case _ConnectionType
Case DBConnType.MSSQL
Dim dbhelper As New CoreMSSQLDataAccessHelper.CDataAccess
dbhelper.ConnectionString = _ConnectionString
Return dbhelper
Case Else
Throw New Exception("This library doesn't support the database '" & _ConnectionType.ToString & "'")
End Select
End Function
You need to add a few more cases to the above method when you work with multiple databases.
Next: Getting a single row or single value from the data access helper >>
More MS SQL Server Articles
More By Jagadish Chaterjee