Microsoft KB Archive/175383

= How To Use ADO in an MTS ATL Component. =

Article ID: 175383

Article Last Modified on 7/13/2004

-

APPLIES TO


 * Microsoft Transaction Services 1.0
 * Microsoft Visual C++ 5.0 Enterprise Edition
 * Microsoft Visual C++ 6.0 Enterprise Edition
 * Microsoft Visual C++ 5.0 Professional Edition
 * Microsoft Visual C++ 6.0 Professional Edition
 * Microsoft Visual C++ 6.0 Standard Edition
 * Microsoft ActiveX Data Objects 1.0
 * Microsoft ActiveX Data Objects 1.5
 * Microsoft ActiveX Data Objects 2.0
 * Microsoft ActiveX Data Objects 2.1

-



This article was previously published under Q175383



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:

 Use the ATL COM AppWizard to create a new DLL. 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."  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.  Use ClassView to open the .cpp file for your method. In this case, the file is named ADOComponent.cpp.</li>  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. </li> 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. 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.</li>  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. </li>  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; </li> 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.</li>  Insert the following code in your method and then compile:

Note You must change UID= and PWD= to the correct values before you run this code. Make sure that UID has the appropriate permissions to perform this operation on the database.

_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= ;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; </li></ol>

<div class="references_section">