HOWTO: Use ADO in an MTS ATL Component.

ID: Q175383

The information in this article applies to:

SUMMARY

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.

MORE INFORMATION

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
   method:

      m_spObjectContext->Release();

   Replace this code with the following line:

      m_spObjectContext.Release();

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;

      try
      {
         _variant_t InVar;

         adoRs.CreateInstance(__uuidof(Recordset));
         adoRs->Open( "select au_fname from authors where "
                      "au_lname = 'White'",
                      "DSN=PUBS;UID=sa;PWD=;",
                      adOpenForwardOnly, adLockReadOnly, adCmdText );
         InVar = adoRs->Fields->GetItem("au_fname")->Value;
         VariantClear(returnval);
         VariantCopy(returnval, &(InVar.Detach()));

         adoRs->Close();
      }
      catch(_com_error)
      {
         if(adoRs)
         {
            adoRs->Close();
            adoRs = NULL;
         }

         m_spObjectContext->SetAbort();
         return z.Error();
      }

      m_spObjectContext->SetComplete();

      return S_OK;

REFERENCES

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

   http://premium.microsoft.com/support/default.asp
   http://www.microsoft.com/transaction/ 

   http://www.microsoft.com/data/ado/ 

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