HOWTO: Use ADO in an MTS ATL Component.

ID: Q175383


The information in this article applies to:
  • Microsoft Transaction Server 1.0
  • Microsoft Visual C++, 32-bit Editions, versions 5.0, 6.0
  • ActiveX Data Objects (ADO), versions 1.0, 1.5, 2.0, 2.1


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 "C:\Program Files\Common Files\SYSTEM\ADO\MSADO15.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. If you plan to deploy this component on Windows 95 or Windows 98 (using Microsoft Transaction Server 2.0) you should choose a non-Unicode configuration.
  8. 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.


  9. If you use Visual C++ version 5.0 then the ATL wizard inserts the following erroneous line into the Deactivate method:
    
          m_spObjectContext->Release(); 
    Replace this code with the following line:
    
         m_spObjectContext.Release(); 


  10. 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.


  11. 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://support.microsoft.com

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:
Q167802 SAMPLE: EXCEPTEX Traps MFC and Win32 Structured Exceptions

Additional query words:

Keywords : kbDatabase kbGrpVCDB kbGrpMDAC TSrvProg kbGrpDSTools
Version : WINDOWS:1.0,1.5,2.0,2.1; winnt:1.0,5.0,6.0
Platform : WINDOWS winnt
Issue type : kbhowto


Last Reviewed: November 4, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.