HOWTO: Use ADO in an MTS ATL Component.

ID: Q175383

The information in this article applies to:


This article explains how to create an Active Template Library (ATL) component which runs under Microsoft Transaction Server (MTS) and uses ActiveX Data Objects (ADO) to manipulate data from SQL Server.


To create an ATL component which runs under MTS and uses ADO to access SQL Server, do the following:

1. Use the ATL COM AppWizard to create a new DLL.

2. Insert a new ATL COM object. Select the MTS type. Click the MTX tab,

   and then enable the following choices:

    - Support IObjectControl
    - Can be Pooled

   For this example, name the object "ADOComponent."

3. In ClassView, right-click the object's interface and choose "Add
   Method." Type the method name, and in the Parameters field type:

      [in, out] VARIANT * returnval

   In this specific example, this argument is not necessary except to
   demonstrate how you might return data to the client.

4. Use ClassView to open the .cpp file for your method. In this case, the
   file is named ADOComponent.cpp.

5. Ensure that the includes listed below are in the ADOComponent.cpp file.

      #import "msado10.dll" no_namespace rename("EOF", "adoEOF" )

   If you plan to reference ADO Objects as arguments to a method that
   is declared in ADOComponent.h, you must move the #import directive
   to that header file.

6. On the Build menu, click "Set Active Configuration" and then choose
   the "Win32 Unicode Release MinSize" configuration.

   Other configurations will run. However, this configuration is ideal for
   MTS on Windows NT.

7. Under Project Settings, click the C/C++ tab and add the /GX flag to
   enable C++ exception handling. Also delete the following directive:

      /D "_ATL_MIN_CRT"

   C++ exception handling is required by ATL smart pointers.

8. The ATL wizard inserted the following erroneous line into the Deactivate


   Replace this code with the following line:


9. Using ODBC administrator, create a DSN on your computer named PUBS.

   This DSN should point to the pubs database on a SQL Server.
   Alternatively, you could change the SQL text in the Open statement to
   process data from another database.

   You may want to make this a System DSN.

10. Insert the following code in your method and then compile:

      _RecordsetPtr adoRs = NULL;

         _variant_t InVar;

         adoRs->Open( "select au_fname from authors where "
                      "au_lname = 'White'",
                      adOpenForwardOnly, adLockReadOnly, adCmdText );
         InVar = adoRs->Fields->GetItem("au_fname")->Value;
         VariantCopy(returnval, &(InVar.Detach()));

            adoRs = NULL;

         return z.Error();


      return S_OK;


For more information about Microsoft Transaction Server or ADO, please consult the following web sites: 

For more information about exception handling, including #import's _com_error exception, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q167802
   TITLE     : SAMPLE: EXCEPTEX Traps MFC and Win32 Structured Exceptions

Additional query words: 1.00 1.50 5.00
Keywords          : TSrvProg kbGrpDSTools 
Version           : WINDOWS:1.0,1.5; WINNT:1.0,5.0
Platform          : WINDOWS winnt
Issue type        : kbhowto
Solution Type     : kbpending

Last Reviewed: October 1, 1998