How to use VC + + to expand ADO __c++

Source: Internet
Author: User
Tags ole
How to use VC + + to extend ADO

IADORecordBinding interface
VC + + links to the extension of ADO or bind a Recordset object to the various fields to C + + variables. When the current row of the bound recordset changes, the values of all the fields that are bound are also copied to the corresponding C + + variable. If necessary, the copied data is also automatically converted to the data type.
The BindToRecordset method of the IADORecordBinding interface binds the field on top of the C + + variable. The AddNew method is to add a new row to the bound recordset. The Update method populates a new row in the recordset or updates an existing row with the value of a C + + variable.
The IADORecordBinding interface is implemented by the Recordset object, and you do not need to encode it yourself to implement it.

Binding entry
VC + + 's extension to ADO image (MAP) between a Recordset object and a C + + variable. The image between a field and a corresponding variable is called a binding entry. Predefined macros provide binding entries for numeric, fixed-length, or indefinite data. All binding entries are encapsulated with the corresponding C + + variables and declared in a class derived from the VC + + extension class cadorecordbinding. This cadorecordbinding class is defined internally by the binding entry macro.
Within ADO, all macro parameters are mapped to an OLE DB dbbinding structure and an OLE DB access (accessor) object is created to manage all behavior and data conversion between fields and variables. OLE DB defines data that consists of a buffer that stores data, a status value that indicates whether a field was successfully deposited in a buffer, or whether the value of the variable was successfully deposited in the field; Data length. (See OLE DB Programmer's Reference Chapter 6th: More about reading and writing data)

Required header Files
In order to use VC + + to expand ADO, you have to include this header file in your application: #include <icrsint.h>

Fields that bind the recordset
To bind the fields of the recordset to the C + + variable, you need to do this:
1. Create a cadorecordsetbinding derived class.
2. Define the binding entry and the corresponding C + + variable in the derived class. Be careful not to use commas and semicolons to cut off macros. Each macro automatically defines the appropriate separator character.
Define a binding entry for each field that is being imaged. And note that according to the different circumstances of the selection of Ado_fixed_length_entry, Ado_numeric_entry, ado_variable_length_entry a macro.
3. In your application, create an instance of the derived class. Get the IADORecordBinding interface from the recordset, and then call the BindToRecordset method to bind all of the recordset's fields to the corresponding C + + variable.
See the sample program for more information.

Interface method
The IADORecordBinding interface has only three methods: BindToRecordset, AddNew, and update. The only parameter required for each method is an instance pointer to a cadorecordbinding derived class. Therefore, the AddNew and Update methods cannot use any of the parameters in the ADO method that they have the same name.

Grammar
The BindToRecordset method binds a field to a C + + variable.
BindToRecordset (cadorecordbinding *binding)
The AddNew method references its ADO function with the same name to add a new line of records.
AddNew (cadorecordbinding *binding)
The Update method also references its ADO function with the same name to update the recordset.
Update (cadorecordbinding *binding)

Binding entry Macros
The binding entry macro defines the corresponding relationship between a recordset field and a variable. The binding macro for each entry consists of a start macro and an end macro and is paired with.
Fixed-length data macros apply to Addate,adboolean and so on, digital macros for Adtinyint, adinteger and addouble, variable-length data macros for Adchar, adVarChar and Advarbinary and so on. All numeric types, in addition to advarnumeric, are fixed-length data types. Each macro has a different group of parameters between the families, so you can exclude the binding information that is not of interest.
See OLE DB Programmer's Reference Appendix A: More information about data types

Start Binding entry
Begin_ado_binding (Class)

Fixed-length data:
Ado_fixed_length_entry (Ordinal, DataType, Buffer, Status, Modify)
Ado_fixed_length_entry2 (Ordinal, DataType, Buffer, Modify)
Digital data:
Ado_numeric_entry (Ordinal, DataType, Buffer, Precision, Scale, Status, Modify)
Ado_numeric_entry2 (Ordinal, DataType, Buffer, Precision, Scale, Modify)
Variable length data:
Ado_variable_length_entry (Ordinal, DataType, Buffer, Size, Status, L Ength, Modify)
Ado_variable_length_entry2 (Ordinal, DataType, Buffer, Size, Status, Modify)
Ado_variable_length_entry3 (Ordinal, DataType, Buffer, Size, LENGTH, Modify)
Ado_variable_length_entry4 (Ordinal, DataType, Buffer, Size, Modify)

End binding
End_ado_binding ()
Parameter description
Class The name of the derived class.
Ordinal a 1-based ordinal that corresponds to a field in the recordset.
DataType the ADO data type that corresponds to C + + variables (see DataTypeEnum to get a list of valid data types). If desired, the value of the field is converted to the value of that type.
Buffer corresponds to the name of a/C + + variable.
Size The maximum number of bytes for this C + + variable. If it's a variable-length string, use a 0 representation.
Status indicates the name of the variable. The variable is used to indicate whether the buffer is valid and whether the data conversion was successful.
A value adFldOK means that the conversion succeeded; adFldNull means that the value of the field is empty. Other possible values are shown in the list of status values that follow.
Modify logical flag. True means that ADO allows the values of fields in the recordset to be updated with variable values.
Setting this value to True will allow for updates if you only want to check the value of the field and do not want to change it so set to false.
Precision the number of digits of a numeric variable.
Scale the number of decimal digits of a numeric variable.
Length is the name of a 4-byte variable. The variable will contain the actual length of the data in the buffer.

Status value
The value of the variable status indicates whether the value of a field has been successfully copied to the corresponding variable. When writing data, you can assign a status value to adFldNull to indicate that the field will be set to null.
Constant Value Description
adFldOK 01 non-null field values are returned.
The Adfldbadaccessor 1 binding is not valid.
The Adfldcantconvertvalue 2 value cannot be converted correctly because of a symbol mismatch or out of bounds.
adFldNull 3 reads the field value, indicates that a null value is returned. The value of the write segment indicates that the field will be set to NULL when the field itself cannot encode null.
Adfldtruncated 4 variable length data or numbers are truncated.
The Adfldsignmismatch 5 value is a signed number, and the data type is unsigned.
Adflddataoverflow 6 data values exceed bounds.
Adfldcantcreate 7 Unknown column types and fields have been opened.
The Adfldunavailable 8 field value cannot be determined. For example, a new unassigned field with no value.
Adfldpermissiondenied 9 was not allowed to update data.
The Adfldintegrityviolation 10 Update field value violates the column's integrity requirements.
The Adfldschemaviolation 11 Update field value violates the specification requirements for the column.
Adfldbadstatus 12 Invalid status parameters when updating a field.
Adflddefault 13 when the field is updated, the default value is used.

An example of using VC + + to extend ADO
In this example, the COM proprietary "smart pointer" feature is also used, which automatically handles the QueryInterface and reference counts of the IADORecordBinding interface. If you don't have a smart pointer, you have to encode this:
IADORecordBinding *picrs = NULL;
...
Testhr (Prs->queryinterface (
__uuidof (iadorecordbinding), (lpvoid*) &picrs));
...
if (Picrs) picrs->release ();
Using smart pointers, you can derive iadorecordbindingptr types from the IADORecordBinding interface with such statements:
_com_smartptr_typedef (IADORecordBinding, __uuidof (iadorecordbinding));
Then instantiate the pointer like this:
Iadorecordbindingptr Picrs (pRs);
Because VC + + extensions are implemented by the Recordset object, the smart pointer Picrs constructor uses the _RecordsetPtr class pointer PRs. The constructor uses the PRS call QueryInterface to obtain the IADORecordBinding interface.

Here is the sample program
#import "C:\Program Files\Common Files\system\ado\msado15.dll" \
No_namespace rename ("EOF", "Endoffile")

#include <stdio.h>
#include <icrsint.h>
_com_smartptr_typedef (IADORecordBinding, __uuidof (iadorecordbinding));

inline void Testhr (HRESULT _hr) {if FAILED (_hr) _com_issue_error (_hr);}

Class Ccustomrs:public CADORecordBinding
{
Begin_ado_binding (Ccustomrs)
Ado_variable_length_entry2 (2, adVarChar, M_ch_fname,
sizeof (M_ch_fname), M_ul_fnamestatus, False)
Ado_variable_length_entry2 (4, adVarChar, M_ch_lname,
sizeof (M_ch_lname), M_ul_lnamestatus, False)
End_ado_binding ()
Public
CHAR m_ch_fname[22];
CHAR m_ch_lname[32];
ULONG M_ul_fnamestatus;
ULONG M_ul_lnamestatus;
};

void Main (void)
{
:: CoInitialize (NULL);
Try
{
_RecordsetPtr pRs ("ADODB"). Recordset ");
Ccustomrs rs;
Iadorecordbindingptr Picrs (pRs);

Prs->open ("SELECT * from Employee ORDER by lname",
"Dsn=pubs;uid=sa;pwd=;",
adOpenStatic, adLockOptimistic, adCmdText);

Testhr (Picrs->bindtorecordset (&rs));

while (!prs->endoffile)
{
Working with data in Ccustomrs
printf ("Name =%s%s\n",
(Rs.m_ul_fnamestatus = = adFldOK Rs.m_ch_fname: "<Error>"),
(Rs.m_ul_lnamestatus = = adFldOK Rs.m_ch_lname: "<Error>"));

When you move to the next line, the value of the new row is automatically populated into the corresponding Ccustomrs variable
Prs->movenext ();
}
}
catch (_com_error &e)
{
printf ("error:\n");
printf ("Code =%08lx\n", E.error ());
printf ("meaning =%s\n", E.errormessage ());
printf ("Source =%s\n", (LPCSTR) E.source ());
printf ("Description =%s\n", (LPCSTR) e.description ());
}
:: CoUninitialize ();
}

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: info-contact@alibabacloud.com 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.