VC + + How programmers should read ADO documents

Source: Internet
Author: User
Tags exception handling

The ADO API Reference uses VB syntax to describe the content of the ADO API. But ADO programmers use different programming languages, such as vb,vc++,vj++. This "ADO for VC + + Syntax Index" provides a detailed description of the syntax specifications of VC + +, including functions, parameters, exception handling and so on.

ADO is implemented on a number of COM excuses, so its use is simpler for a programmer who is doing COM programming. For example, almost all the use of COM details for VB programmers are hidden, but for VC + + programmers should pay special attention to. The following is an overview of the use of the ADO and #import indicators for C and C + + programmers, mainly describing the data types used by COM (variants, BSTR, and SafeArray), and handling of exceptions (_com_error).

Using the #import compilation indicator

#import编译指示符使使用ADO的方法与属性简单化. This indicator requires a type library filename, such as ADO.dll (Msado15.dll), and generates the corresponding header file, which includes the defined type, an intelligent pointer to the interface, and a constant. And all the interfaces are encapsulated into classes.

For each action in the class (or method, property call), there is a declaration to ensure that it can be called directly (or as the source form of the operation), and another declaration to invoke the source operation and throw a COM error when the operation fails. If the action is an attribute, the compilation indicator can create an interactive, VB-like syntax for the operation.

Returns/sets the operation of the property has a corresponding formal name-getproperty/putpropert, while setting a pointer to an ADO object property value is PutRefProperty. You will use the following form to read and write the value of the property:

variable = Objectptr->getproperty (); Reading the value of a property

Objectptr->putproperty (value); Set the value of a property

Objectptr->putrefproperty (&value); Sets the value of a pointer-type property

Using properties directly

__declspec (property ...) The compile indicator is a Microsoft-defined extension of the C language that makes a function work like a property.        This way you can use the following syntax form to read and write the value of a property using vb: Objectptr->property = value; Set the value of a property

variable = objectptr->property; Reading the value of a property

__declspec (property ...) The compilation indicator can only be used for a property's read-write function and automatically generates the corresponding call form, depending on whether the property is available for reading or writing. Each property may have GetProperty, putproperty,putrefproperty three functions, but the compiler can only generate two forms of interaction. For example, the ActiveConnection property of a Command object has two read-write functions, Getactiveconnection and putrefactiveconnection. While the putref-form is a good choice in practice, you can save a pointer to an active connection object in this property. The Recordset object, on the other hand, has get-, put-, and putrefactiveconnection operations, but does not have an interactive syntactic form.

Collections,getitem method and Item property

ADO defines several sets of collection, including Fields,parameters,properties, and errors. In Visual C + +, the GetItem (index) method returns a member of the collection. Index is a Variant parameter that can be either a corresponding ordinal of that member or a string that includes its name.

__declspec (property ...) The compilation indicator generates a direct use form for the Item property corresponding to the GetItem () method (the interactive syntactic form mentioned above). This form is similar to using the syntax form of [] when referencing an array element:

Collectionptr->getitem (index);


For example, to assign a value to a field in RS of a Recordset object that derives from the authors table in the pubs database. Use the item () property to access the third field in the Fields collection of this recordset (the collection is always numbered from 0, assuming the third field is named Au_fname). The value () method is then invoked to assign a string value to the field.

The syntax form of Visual Basic:

Rs. Fields.item (2). Value = "Value"

Rs. Fields.item ("au_fname"). Value = "Value"


RS (2) = "Value"

Rs!au_fname = "Value"

Syntax forms of Visual C + +:

Rs->fields->getitem (2)->putvalue ("value");

Rs->fields->getitem ("au_fname")->putvalue ("value");


Rs->fields->item[2]->value = "Value";

rs->fields->item["au_fname"]->value = "Value";

COM-specific data types

Generally, you see in the "ADO API Reference" in the VB data type in VC + + can also find the corresponding type. These include standard data types, such as unsigned char corresponding to the byte,short corresponding to VB Integer,long corresponding to long. See "Syntax Indexes" for more details on the required operands.

As an exception, the data types that are used exclusively for COM are: Variant, BSTR, and SafeArray.


A variant is a structured data type that contains a representation of a member value and its data type. A variant can represent quite a number of data types, or even another variant, BSTR, Boolean, IDispatch or IUnknown pointer, currency, date, and so on. COM also provides a number of ways to make conversion between data types more simplistic.

The _variant_t class encapsulates and manages a variant of this data type.

When "ADO API Reference" says a method or attribute to use a parameter, it usually means that a _variant_t type parameter is required. This rule is clearly stated in the parameters chapter of the ADO API Reference. As an exception, it is sometimes required that the operand be a standard data type, such as long or byte, or an enumeration value. Another exception is the requirement that the operand be a string of strings.


BSTR (Basic string) is also a structured data type, including the length of strings and strings. COM provides a way to allocate, manipulate, and release strings of space.

The _bstr_t class encapsulates and manages the BSTR data type.

When "ADO API Reference" says a method or attribute to use a string parameter, it usually means that a class-_bstr_t parameter is required.

Forced type conversions for _variant_t and _bstr_t classes

Typically, you do not need an explicit type conversion code when passing a _variant_t or _bstr_t parameter to an operation. If the _variant_t or _bstr_t class provides a constructor that corresponds to the parameter type, the compiler will automatically generate the appropriate _variant_t or _bstr_t values.

However, when the arguments are ambiguous, when multiple constructors are corresponding, you must explicitly call the correct constructor to get the correct arguments. For example, the function declaration of the Recordset::Open method is as follows:


Const _VARIANT_T & Source,

Const _variant_t & ActiveConnection,

Enum CursorTypeEnum CursorType,

Enum LockTypeEnum LockType,

Long Options);

Where the parameter ActiveConnection is a reference to a variant_t variable, it can be a connection string or a pointer to an open connection object.

The correct _variant_t type parameter will be constructed regardless of whether you are passing a similar "dsn=pubs;uid=sa;pwd=;" Such a string, or a pointer similar to "(IDispatch *) Pconn".

Alternatively, you can explicitly write code such as "_variant_t (IDispatch *) Pconn, True)" to pass a _variant_t variable that contains pointers. The coercion type conversion (IDispatch *) Here avoids the ambiguity of the possible invocation of the IUnknown interface constructor.

Although it is rarely mentioned, it is particularly important that ADO is always a IDispatch interface. Any pointer that is passed in the contained variant must be converted to a IDispatch interface pointer.

The last thing to note is that the second logical parameter of the constructor is optional, and its default value is true. This parameter determines whether the variant constructor calls the inline AddRef () method and whether the _variant_t::release () method is automatically invoked after the ADO method or property call is completed


SafeArray is also a structured data type that consists of an array of data elements of other data types. It is called a secure array because it contains the boundary information for each dimension and restricts access to the array elements within the bounds.

When "ADO API Reference" says a method or attribute to use or return an array, it usually means a SAFEARRAY array, not a localized C + + array.

For example, the second parameter of the connection object's OpenSchema method requires an array of variant values. These variant values must be passed as elements of a SAFEARRAY array. And the SAFEARRAY array itself is passed as a variant.

Further, the first parameter of the Find method is an optional first and second parameter of the Variant;addnew method that points to a one-dimensional SAFEARRAY array, also a one-dimensional SAFEARRAY array The return value of the GetRows method is a variant that contains a two-D SAFEARRAY array.

Default parameters

VB allows you to omit some of the parameters of the method. For example, the Recordset object's Open method has five parameters, but you can skip the middle argument and omit the argument. Omitted arguments are replaced with BSTR or variant defaults that are automatically created.

All operands must be defined in C/A + +. If you want to define a string-type default parameter, define a _bstr_t that contains an empty string. If you want to define a variant default parameter, define a _variant_t with a value of Disp_e_paramnotfound and type Vt_error. You can also use the #import compilation indicator to provide the equivalent constant vtmissing.

The use of vtmissing has three kinds of unexpected situations:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.