Skip navigation

Using COM Components with MTS

Exploring COM components

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

  1. Start Visual Basic and create a new project of type ActiveX DLL
  2. Open the project Properties window by selecting Properties from the Project menu or right-clicking the project name in Project Explorer and selecting Properties
  3. 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.
  4. Click the Components tab, and click the Project Compatibility radio button.
  5. Click OK to close the property pages.
  6. Rename the default class module RecordSetStuff and set its instancing property to multiuse.
  7. Add the code in Listing 1 to this class.
  8. Create a new module by selecting Add Module from the Project menu.
  9. Rename the module to Passwords.
  10. 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.
  11. Add references to support database connectivity.
  12. Open the References Dialog from the Project menu and select the Microsoft ActiveX Data Objects 2.1 Library.
  13. Save the project by clicking Save Project on the File menu.
  14. Compile the project using the Make command on the File menu.

Creating the Vendors Component

  1. Add a new project of type ActiveX DLL by selecting Add Project from the File menu.
  2. Open the project Properties window by selecting Properties from the Project menu or right-clicking the project name in Project Explorer and selecting Properties.
  3. Name the project Vendor and click the Unattended Execution check box.
  4. Rename the default class module Publisher and be sure that its Instancing property is set to multiuse.
  5. Open the Class Builder utility from the Add-Ins menu.
  6. In the left pane of the Class Builder, click the Publisher class once to select it.
  7. Click the Add New Method button on the toolbar to add a new method.
  8. Enter RetrieveAllPublishers as the name.
  9. Select Variant as the return type. Screen 1 shows the Add New Method dialog box that will appear.
  10. Click OK.
  11. Click the Add New Method button on the toolbar to add a new method.
  12. Enter RetrievePublisherbyID as the name.
  13. Select Variant as the return type.
  14. Click the + sign to add an argument.
  15. Enter Pub_ID as the name.
  16. Click the ByVal checkbox.
  17. Select String as the data type. Screen 2 shows the Add Argument dialog box.
  18. Click OK.
  19. Click OK to close the Method Builder dialog box.
  20. 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 Variant
    End Function
    
    Public Function RetrieveAllPublishers() As Variant
    End Function
    This is the interface for the class. Now you can add the code to implement the interface.
     
  21. To add references to support database connectivity, open the References dialog box from the Project menu and select the DBStuff reference.
  22. Add the code in Listing 3 to the RetrieveAllPublishers method.
  23. Add the code in Listing 4 to the RetrievePublisherbyID method.
  24. Save the project by clicking Save Project on the File menu.
  25. Compile the project by selecting Make command on the File menu.

Testing the Vendor Component

  1. Add a new project of type Standard EXE Project by selecting Add Project from the File menu.
  2. Double-click Form1 in the Project Explorer to open it. The form has three buttons and three textboxes. Screen 3 shows the results.
  3. 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.
  4. 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
  5. Double-click the Clear Box button to open the code editor for the form.
  6. Enter the following code at the top of the code window:
    Option Explicit
    Dim oRS As ADODB.Recordset
    Dim oCon As ADODB.Connection
    Dim i As Integer
  7. Next, add a reference to the Vendor project by using the References dialog box from the Project menu.
  8. Select the cmdClear object from the left-hand list in the code editor. Select the Click event from the right-hand list.
  9. Enter the following code in the Click event procedure:
       txtOutput.Text = ""
  10. Select the cmdGetPublisherByID object from the list and create its Click event. Enter the code in Listing 5 in the Click event procedure.
  11. Select the cmdRetrieveAllPublishers object from the list and create its Click event. Enter the code in Listing 6 in the Click event procedure.
  12. Save the project.
  13. 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.

TAGS: SQL
Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish