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 AsObject'can be used flexibly in any of the sub-classes
Private arTransCommands As ArrayList
Private _ConnectionString AsString
Private _ConnectionType As DBConnType
PublicSubNew(ByVal ConnectionType As DBConnType, ByVal ConnectionString AsString)
Try
_ConnectionString = ConnectionString
_ConnectionType = ConnectionType
If Len(Trim(_ConnectionString & "")) = 0 ThenThrowNew Exception("Connection String not configured")
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() AsObject'can be used flexibly in any of the sub-classes
SelectCase _ConnectionType
Case DBConnType.MSSQL
Dim dbhelper AsNew CoreMSSQLDataAccessHelper.CDataAccess
dbhelper.ConnectionString = _ConnectionString
Return dbhelper
CaseElse
ThrowNew Exception("This library doesn't support the database '" & _ConnectionType.ToString & "'")
EndSelect
EndFunction
You need to add a few more cases to the above method when you work with multiple databases.
You can observe that I am creating an object of the data helper type in “getDBHelperObject.” I also need to dispose of the same when not in use. If you don’t dispose of it, it will not be released and it will never return back to the pool. To make the pool always reusable, you need to dispose of the COM+ object immediately after finishing your work.
You can also increase the size of the pool by modifying the attribute value given in previous articles.
To work with the above methods, you need to first start the transaction by simply calling the method “TransactionBegin.” You can add as many commands as possible by using “TransactionAddCommand.” After adding all the transaction commands, you simply execute all of them at the same time by issuing the “TransactionCommit” command.
Please note that I didn’t add any stored procedures to work with transactions.
Adding SQL Server stored procedure parameter support to the wrapper
As the parameters of a stored procedure would be a bit different for every database, I started developing a different class especially for SQL Server by extending the same from the above wrapper class.
The class definition would look something like the following:
PublicClass MSSQLDBLib
Inherits DBLib
To work with data types, I include the following enumeration:
PublicEnum MSSQLDataType
SQLString
SQLChar
SQLInteger
SQLBit
SQLDateTime
SQLDecimal
SQLMoney
SQLImage
SQLFloat
EndEnum
I also included the following members to work with stored procedures:
Private dtParameterList AsNew DataTable
Private arParameterList AsNew ArrayList
The source code for MSSQLDBLib is included as part of the download. You can download it and use it for yourself.
The entire series was created only for understanding the development of data helpers with object orientation. Before trying to use this data helper in your applications, I request that you go through the Microsoft Data Access Blocks, which include several features.
Any feedback, suggestions, bugs, errors, improvements etc., are highly appreciated at jag_chat@yahoo.com.