FILE: Adovc.exe Demonstrates How to Use ADO with Visual C++

ID: Q174565

The information in this article applies to:

SUMMARY

Adovc.exe is a sample that demonstrates using ActiveX Data Objects (ADO) within Visual C++ 5.0.

MORE INFORMATION

The following file is available for download from the Microsoft Software Library.

Note: After you click on the following link, please choose "Save this program to disk" from the File Download dialog box. Then, click Run on the Start menu and run the .exe file with the -d attribute, as in the following example:

   C:\...\Adovc.exe -d

 ~ Adovc.exe (size: 312617 bytes) 

For more information about downloading files from the Microsoft Software Library, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q119591
   TITLE     : How to Obtain Microsoft Support Files from Online Services

What ADOVC Demonstrates

ADOVC is a sample that demonstrates using ActiveX Data Objects (ADO) within Visual C++ 5.0. Demonstration code for each of ADO's six classes is presented, with some additional code for handling more common issues such as output/return parameters and error/exception handling.

This sample actually has three separate projects, one for each of the three techniques that can be used with ADO inside Visual C++:

Three separate but nearly identical projects demonstrate each of these techniques. The projects were written so that WinDiff could be used on each directory. This helps identify the files that are different for each implementation, and then makes easy comparison to see what differences in the code each implementation imposes.

Three files in each sample contain all ADO specific code:

   Adoutils.h   - Contains the declaration of error/exception functions,
                  helper functions, and is where ADO declarations are
                  included in the project.
   Adoutils.cpp - Implementation of error/exception and helper functions.
   Adocore.cpp  - Actual ADO sample code, where ADO is demonstrated.

Advantages of Using #import

Disadvantages of Using #import

Advantages of Using MFC-OLE

An advantage of using MFC-OLE is that it hides BSTR and VARIANTS from implementor, methods that return these types are modified so that the actual wrapper class returns a CString.

Disadvantages of Using MFC-OLE

Here are some disadvantages of using MFC-OLE:

- Wrappers derived from COleDispatchDriver do not return HRESULTS. It is
  possible to receive failed HRESULTS bundled inside a ColeDispatchDriver
  exception, but there are scenarios where HRESULTS may not be received.
  It is also important that the exception handling, if it uses the
  Connection object's Errors collection, not process this collection
  unless you have a valid connection object. Otherwise it may be possible
  to generate exceptions within your exception handler.

Advantages of Using OLE-SDK

Disadvantages of Using OLE-SDK

Here are some disadvantages of using OLE-SDK:

#import and the Advantage of "VB-Like Syntax"

Consider the following three code fragments to get the ActualSize property of a Field object, starting from the Recordset object:

    // #import
    CString    strTmp;

    strTmp.Format( "\t\t...Actual Size  = %ld",
                   Rs1->Fields->Item[_variant_t((long)0)]->ActualSize );

One line of code to go from Recordset to the ActualSize property. Now consider MFC-OLE:

   // MFC-OLE
   CString     strTmp;
   Fields      Flds1
   Field       Fld1;

   Flds1 = Rs1.GetFields();
   Fld1  = Flds1.GetItem( COleVariant( (long) 0 ) );
   strTmp.Format( "\t\t...Actual Size  = %ld", Fld1.GetActualSize );

Finally, consider the OLE-SDK equivalent code, which is nearly the same size as MFC-OLE, but gives more immediate error handling results than MFC- OLE is capable of.

   // OLE-SDK with ADOID.H and ADOINT.H
   CString     strTmp;
   ADOFields*  Flds1   = NULL;
   ADOField*   Fld1    = NULL;
   long        nTmp;

   if( SUCCEEDED ( hr ) ) hr = Rs1->get_Fields( & Flds1 );
   if( SUCCEEDED ( hr ) ) hr = Flds1->get_Item( COleVariant((long) 0 ),
                                                &Fld1 );
   if( SUCCEEDED ( hr ) ) hr = Fld1->get_ActualSize( &nTmp );
   if( SUCCEEDED ( hr ) ) strTmp.Format( "\t\t...Actual Size  = %ld",
                                         nTmp );

#import vs. COleDispatchDriver vs. OLE SDK

Based on the three projects in this sample, the recommended preference for using ADO in Visual C++ follows:

1. #import because it dramatically simplifies using COM

2. OLE SDK because you get back HRESULTS that are otherwise buried in MFC-

    OLE (when OLE SDK breaks, you know it)

3. MFC-OLE, despite being an intermediate step towards #import, hides too
    much when it doesn't give you back a failed HRESULT

Other Tips for Using VC and ADO

About the ADO* Series of Samples

This is one of several identical samples implemented using ADO within various products, as listed below. The advantage is that these articles have an identical interface/functionality, demonstrating both the similarities differences in using ADO with different languages and with different mechanisms within that language (where applicable).

QNumber      Title
Q172403      FILE: Adovb.exe Demonstrates How to Use ADO with Visual Basic
Q174565      FILE: Adovc.exe Demonstrates How to Use ADO with Visual C++
Q182782      FILE: Adovj.exe Demonstrates How to Use ADO with Visual J++
Q185033      FILE: Adoacc.exe Demonstrates Using ADO with Access 97

Other Knowledge Base Articles of Note

These articles provide detailed information not covered in this text about using ADO with various Visual C++ implementation mechanisms.

   QNumber                           Title
   -------  ----------------------------------------------------------
   Q169496  INFO: Using ActiveX Data Objects (ADO) via #import in VC++
   Q175993  INFO: Using ActiveX Data Objects (ADO) via MFC OLE in VC++
   Q176342  INFO: Using ActiveX Data Objects (ADO) via OLE SDK in VC++

ADO Version 1.5

At the time of the writing of this sample, ADO 1.5 was not yet released, but was available publicly in beta. This sample has been written to work with either version, but there are some differences:

Keywords          : kbsample kbADO kbVC 
Version           : WINDOWS:1.0
Platform          : WINDOWS

Last Reviewed: February 4, 1999