Using COM Components with MTS
Exploring COM components
May 31, 2000
This article is the first in a series about using Visual Basic (VB) to develop distributed applications. I’ll explore COM components and how you can use them with Microsoft Transaction Server (MTS). In subsequent articles, I’ll discuss COM+ in Windows 2000.
First Things First
First, let’s create a couple of simple components that will work together to provide access to data in SQL Server. The first component, DBStuff, is a general-purpose database class. It contains the class RecordSetStuff, which works with ADO recordsets. The other component is Vendor, which in this implementation contains only the Publisher class. The Publisher class can return either one publisher by ID or all publishers. The application also uses a standard executable project to test the components.
Creating the DBStuff Component
Start Visual Basic and create a new project of type ActiveX DLL
Open the project Properties window by selecting Properties from the Project menu or right-clicking the project name in Project Explorer and selecting Properties
Name the project DBStuff and click the Unattended Execution check box, which specifies that the project is intended to run without user interaction. When you select Unattended Execution, any runtime functions, such as messages, that normally result in user interaction are written to an event log.
Click the Components tab, and click the Project Compatibility radio button.
Click OK to close the property pages.
Rename the default class module RecordSetStuff and set its instancing property to multiuse.
Add the code in Listing 1 to this class.
Create a new module by selecting Add Module from the Project menu.
Rename the module to Passwords.
Add the code in Listing 2 to the Passwords module. You’ll need to change the three constants to match the appropriate values for your database.
Add references to support database connectivity.
Open the References Dialog from the Project menu and select the Microsoft ActiveX Data Objects 2.1 Library.
Save the project by clicking Save Project on the File menu.
Compile the project using the Make command on the File menu.
Creating the Vendors Component
Add a new project of type ActiveX DLL by selecting Add Project from the File menu.
Open the project Properties window by selecting Properties from the Project menu or right-clicking the project name in Project Explorer and selecting Properties.
Name the project Vendor and click the Unattended Execution check box.
Rename the default class module Publisher and be sure that its Instancing property is set to multiuse.
Open the Class Builder utility from the Add-Ins menu.
In the left pane of the Class Builder, click the Publisher class once to select it.
Click the Add New Method button on the toolbar to add a new method.
Enter RetrieveAllPublishers as the name.
Select Variant as the return type. Screen 1 shows the Add New Method dialog box that will appear.
Click OK.
Click the Add New Method button on the toolbar to add a new method.
Enter RetrievePublisherbyID as the name.
Select Variant as the return type.
Click the + sign to add an argument.
Enter Pub_ID as the name.
Click the ByVal checkbox.
Select String as the data type. Screen 2 shows the Add Argument dialog box.
Click OK.
Click OK to close the Method Builder dialog box.
Close the Class Builder by clicking the X in the upper right corner of its window or selecting Exit from the File menu. When prompted, answer Yes to the Update project with changes question. In the editor, your class will now look like:
Public Function RetrievePublisherbyID(ByVal pub_id As String) As VariantEnd FunctionPublic Function RetrieveAllPublishers() As VariantEnd Function
This is the interface for the class. Now you can add the code to implement the interface.
To add references to support database connectivity, open the References dialog box from the Project menu and select the DBStuff reference.
Add the code in Listing 3 to the RetrieveAllPublishers method.
Add the code in Listing 4 to the RetrievePublisherbyID method.
Save the project by clicking Save Project on the File menu.
Compile the project by selecting Make command on the File menu.
Testing the Vendor Component
Add a new project of type Standard EXE Project by selecting Add Project from the File menu.
Double-click Form1 in the Project Explorer to open it. The form has three buttons and three textboxes. Screen 3 shows the results.
Add the textboxes. The top box is txtStatus. Click the Textbox tool in the Toolbox, then click the mouse on the form and drag the textbox to size it. You can press F4 to enter the field’s name. The small textbox on the right of the form is txtPubID. The large textbox is txtOutput.
The Clear Box button is a command button. Click the CommandButton tool in the Toolbox, then click the mouse on the form and drag the button to size it. You can press F4 to enter the button’s name, which is cmdClear. You can enter the Clear Box description in the Caption property. The All Publisher button is named cmdRetrieveAllPublishers. The All Publisher button is named cmdGetPublisherByID
Double-click the Clear Box button to open the code editor for the form.
Enter the following code at the top of the code window:
Option ExplicitDim oRS As ADODB.RecordsetDim oCon As ADODB.ConnectionDim i As Integer
Next, add a reference to the Vendor project by using the References dialog box from the Project menu.
Select the cmdClear object from the left-hand list in the code editor. Select the Click event from the right-hand list.
Enter the following code in the Click event procedure:
txtOutput.Text = ""
Select the cmdGetPublisherByID object from the list and create its Click event. Enter the code in Listing 5 in the Click event procedure.
Select the cmdRetrieveAllPublishers object from the list and create its Click event. Enter the code in Listing 6 in the Click event procedure.
Save the project.
Run the project by pressing F5 or clicking the run button on the toolbar. You can test the components by clicking the All Publishers button. You can vary the results by using the A Publisher button to retrieve one publisher. You must enter a publisher ID for this button to work.
Simple and Fast
This simple article demonstrates how to create and work with two COM components. What is not apparent is the speed increase. The components will increase the speed of your Web site when you build it with Active Server Pages (ASP). Using a component to do the database work for you (such as DBStuff) removes the ADO code from the ASP code and can dramatically speed the operations of the Web application.
About the Author
You May Also Like