Microsoft KB Archive/174565: Difference between revisions
m (Text replacement - "[[mk:@MSITStore:kbdatacc.chm::/Source/adobj/q" to "[[../") |
m (Text replacement - """ to """) |
||
Line 31: | Line 31: | ||
The following file is available for download from the Microsoft Software Library.<br /> | The following file is available for download from the Microsoft Software Library.<br /> | ||
<br /> | <br /> | ||
'''Note''': After you click on the following link, please choose | '''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: | ||
<blockquote>C:\...\Adovc.exe -d<br /> | <blockquote>C:\...\Adovc.exe -d<br /> | ||
Line 59: | Line 59: | ||
=== Advantages of Using #import === | === Advantages of Using #import === | ||
* #import offers very Visual Basic-like syntax for using ADO, easing much of the sting of | * #import offers very Visual Basic-like syntax for using ADO, easing much of the sting of "raw" COM. This can't be stressed enough, as it greatly simplifies referencing a specific item within a collection of a specific object. Consider the code fragment shown below and compare it to similar ADO coding techniques. | ||
* _bstr_t and _variant_t both make handling BSTR and VARIANT types relatively painless. | * _bstr_t and _variant_t both make handling BSTR and VARIANT types relatively painless. | ||
* Failed HRESULT results in an exception being raised. _com_error automates the task of querying the IErrorInfo interface to acquire details of the error. | * Failed HRESULT results in an exception being raised. _com_error automates the task of querying the IErrorInfo interface to acquire details of the error. | ||
Line 97: | Line 97: | ||
* The actual sample code uses MFC classes, COleVarient and CString, to facilitate coding. A pure OLE-SDK application would not take advantage of the CString::AllocSysString() method. (However, if you don't want to use CString, this method's source is an excellent place to get a handy helper function to convert between char* and BSTR). | * The actual sample code uses MFC classes, COleVarient and CString, to facilitate coding. A pure OLE-SDK application would not take advantage of the CString::AllocSysString() method. (However, if you don't want to use CString, this method's source is an excellent place to get a handy helper function to convert between char* and BSTR). | ||
=== #import and the Advantage of | === #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: | Consider the following three code fragments to get the ActualSize property of a Field object, starting from the Recordset object: | ||
Line 103: | Line 103: | ||
CString strTmp; | CString strTmp; | ||
strTmp.Format( | strTmp.Format( "\t\t...Actual Size = %ld", | ||
Rs1->Fields->Item[_variant_t((long)0)]->ActualSize ); </pre> | Rs1->Fields->Item[_variant_t((long)0)]->ActualSize ); </pre> | ||
One line of code to go from Recordset to the ActualSize property. Now consider MFC-OLE: | One line of code to go from Recordset to the ActualSize property. Now consider MFC-OLE: | ||
Line 113: | Line 113: | ||
Flds1 = Rs1.GetFields(); | Flds1 = Rs1.GetFields(); | ||
Fld1 = Flds1.GetItem( COleVariant( (long) 0 ) ); | Fld1 = Flds1.GetItem( COleVariant( (long) 0 ) ); | ||
strTmp.Format( | strTmp.Format( "\t\t...Actual Size = %ld", Fld1.GetActualSize ); </pre> | ||
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. | 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. | ||
<pre class="CODESAMP"> // OLE-SDK with ADOID.H and ADOINT.H | <pre class="CODESAMP"> // OLE-SDK with ADOID.H and ADOINT.H | ||
Line 126: | Line 126: | ||
&Fld1 ); | &Fld1 ); | ||
if( SUCCEEDED ( hr ) ) hr = Fld1->get_ActualSize( &nTmp ); | if( SUCCEEDED ( hr ) ) hr = Fld1->get_ActualSize( &nTmp ); | ||
if( SUCCEEDED ( hr ) ) strTmp.Format( | if( SUCCEEDED ( hr ) ) strTmp.Format( "\t\t...Actual Size = %ld", | ||
nTmp ); </pre> | nTmp ); </pre> | ||
=== #import vs. COleDispatchDriver vs. OLE SDK === | === #import vs. COleDispatchDriver vs. OLE SDK === |
Revision as of 11:07, 21 July 2020
The information in this article applies to:
SUMMARYAdovc.exe is a sample that demonstrates using ActiveX Data Objects (ADO) within Visual C++ 5.0.
MORE INFORMATIONThe following file is available for download from the Microsoft Software Library.
For more information about downloading files from the Microsoft Software Library, please see the following article in the Microsoft Knowledge Base:
What ADOVC DemonstratesADOVC 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.
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. 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-OLEAn 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-OLEHere are some disadvantages of using MFC-OLE:
Advantages of Using OLE-SDK
Disadvantages of Using OLE-SDKHere 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 SDKBased on the three projects in this sample, the recommended preference for using ADO in Visual C++ follows:
Other Tips for Using VC and ADO
About the ADO* Series of SamplesThis 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 Other Knowledge Base Articles of NoteThese 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++ ADO Version 1.5At 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:
Additional query words: Keywords : kbsample kbADO kbVC Version : WINDOWS:1.0 Platform : WINDOWS Issue type : |
Last Reviewed: October 6, 1999 |