Understand Visual C ++ Extensions for ADO

zhaozj2021-02-08  248

[Foreword]

When we use Visual C for ADO programming, a quite headache work is the processing of the Variant field type. Usually, first convert the Variant type into a similar C type in the form, then put the converted data in a class (Class) or Structure). Even so, the processing of the Variant data type also affects the performance of the program to some extent.

ADO provides us with an interface that allows us to read data directly to the local, thereby winding the processing for complex Variant data types. At the same time, ADO also defines a set of preprocessing macros to simplify the use of the interface. Using this tool, we will make our programming work easily and efficient.

In general, we get the Recordset dataset from Ado, then define a C / C structure type, and then bind the records in the Recordset to the structural member variable. When you encounter a Variant type, the situation is complicated, you must solve how to convert the Variant data type (database) to the C / C data type (local) problem. Visual C Extensions For Ado (for the narrative, here, ADOEXT) is to make it easier.

[IadorecordBinding interface introduction]

AdoExt binds the field in the Recordset record to the C / C variable. Once the data of the Recordset changes, the data will be immediately copied to the binded C / C variable. Data will be converted to the specified C / C data type as needed.

The BindtorecordSet members of the Iadorecordbinding interface is used to implement the database field to the binding between local C / C variables. If you want to add a record for RecordSet, you can use the AddNew method. Update methods are used to update and upgrade the bound C / C variable data.

The implementation of the Iadorecordbinding interface does not have to worry, the Recordset object is quietly completed after the scene.

[Binding Entries) Introduction]

AdoExt maps the field type of the Recordset object to a local C / C variable, and we are defined between the process defined between a database field to a C / C variable is called a binding entry. Binds are completed by macros, and the types that can be bound include numerical, fixed length, and variable length data. The basic process of binding is: Define derived from CadorecordBinding classes that are also a set of macros in the CadorecordBinding class itself, using specific macros to implement data binding in the class. Then, declare the corresponding C / C variables in the class.

The ADO maps the parameters in the macro of the macro to an OLE DB DBINDING type structure and creates an OLE DB access object to manage data movement and format conversions between fields and variables. The data definition of OLE DB includes three parts: a buffer for storing data; a status bit for indicating data access status, and how variables extracted; and the length of the data.

[Header file contains] To use Visual C AdoExt, you need to include the following file in the application: #include

[Procedure for binding the Recordset field] ■ Create a class derived from CadorecordSetBinding. ■ Set the binding unit in the derived class and define the corresponding C / C variable. These bindings are defined between Begin_ADO_BINDING and END_ADO_BINDING. Don't want to place a comma or semicolon between the macro definition, which will be automatically implemented inside the macro.

■ Specify a binding unit for each database field to be mapped to C / C variables. Select one from ADO_FIXED_LENTRY, or ADO_NUMERIC_ENTRY, or ADO_VARIABLE_ENTRY, or Ado_variable_length_entry, and fill in the correct parameters.

■ In your reference, create an instance of this class. Get the IadorecordBinding interface from the Recordset. Then call the BindtorecordSet method to implement data binding.

[Interface Method Definition] There are three methods for the Iadorecordbinding interface: Bindtorecordset, addNew, and Update. These methods have only one pointer type parameter, which points to a class instance derived from CadorecordBinding. In fact, the addNew and Updage methods will call the same name method in the ADO.

grammar:

The BindtorecordSet method implements the binding between the Recordset field to the C / C variable.

BindtorecordSet (Cadorecordbinding * Binding)

The AddNew method calls its same name method, the AddNew method in the ADDNew method, add a new record to the Recordset.

AddNew (Cadorecordbinding * Binding)

The UPDATE method calls it to the same name method Update in the ADO to complete the data update to the Recordset.

Update (cadorecordbinding * binding)

[Understanding the macro in the binding unit defines the connection between the RECORDSET field and the variable. Macro Begin_ADO_BINDING (Class) and End_ado_binding () (end macro) defines a set of binding units.

The macros in the binding unit provide support for the following field types: ■ Data of the fixed length type. For example addate or adboolean ■ numeric data. For example, ADTINYINT, AdINteger, and Addouble ■ Variable length type data. For example, Adchar, Advarchar, and Advarbinary

All numeric data, in addition to the AdvarNumeric type, is also a fixed length type of data.

Different macros define different types of parameters so you can exclude binding information that is not interested.

■ Start data binding

Begin_ado_binding (class)

■ Data of fixed length type

ADO_FIXED_LENGTH_ENTRY (Ordinal, DataType, Buffer, Status, Modify) ado_fixed_length_entry2 (Ordinal, Datatype, Buffer, Modify)

■ Numerical data

ADO_NUMERIC_ENTRY (Ordinal, Datatype, Buffer, PRecision, Scale, Status, Modify) ado_numeric_entry2 (Ordinal, Datatype, Buffer, Precision, Scale, Modify)

■ Data of variable length type

ADO_VARIABLE_LENGTH_ENTRY (Ordinal, DataType, Buffer, Size, Status, Length, 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 data binding

END_ADO_BINDING ()

-------------------------------------------------- -Parameter Description

Class: The class definition of the binding unit and the C / C variable. Ordinal: The number of sequences, the RECORDSET field number of the count starts, which corresponds to the specified C / C variable. DataType: and the data type of the data type in the ADO equivalent C / C data type. The corresponding Recordset field will be converted to the data type when needed. Buffer: The name of the buffer used to store the Recordset field. SIZE: The maximum size of the buffer. Status: Status Bit. Indicates whether the validity of the content of the buffer is successful, and whether the field conversion is successful. There are two important values. One is Adfldok, indicating that the conversion is successful; the other is AdfldNull, indicating that the field value is NULL. For more status values, please refer to MSDNModify: Boolean type. If true, it indicates that the ADO allows the data in the buffer. If false, it indicates that the data is read-only. Precision: The accuracy of numeric types. Scale: Numeric size of the number. Number of Decimal Places In a Numeric Variable. Length: A four-byte variable. The actual length used to include data in the buffer. -------------------------------------------------- - [Visual C EXTENSIONS routine] // introduces the msado15.dll file, which declares the ADO type library #import "c: / program files / common files / system / ado / msado15.dll" no_namespace rename ("EOF", "Endoffile")

#include #include // contains ICRSINT.H header files here

// _ COM_SMARTPTR_TYPEDEF macro defines a _com_ptr_t type smart (smart) pointer iporecordbingingptr_com_smartptr_typed, __UUIDBINDING, __UUIDOF (Iadorecordbinding);

/ / Define a test function inline void testhr (HRESULT _HR) {if failed (_hr) _com_issue_error (_hr);

// definition of a class derived from CADORecordBinding class CCustomRs: public CADORecordBinding {// begin data type binding BEGIN_ADO_BINDING (CCustomRs) // the 2nd binding Recordset adVarChar type field to the C / C variable m_ch_fname on 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 binding END_ADO_BINDING () // macro defined immediately above used C / C variable public: char m_ch_fname [22]; char m_ch_lname [32]; ulong m_ul_fnamestatus; ulong m_ul_lnamestatus;};

// Start program execution void main (void) {:: Coinitialize (null); // Initialization COM object try {// typef _com_ptr_t <_recordset, __UUIDOF (_Recordset)> _recordset> Intelligent pointer to a _Recordset _RecordSetptr PRS (" AdoDb.recordset "); // Defines an instance of a CCUSTOMRS: Public CadorecordBinding class; // Specify the IadoreCordBindingPTR interface type object PICRS to the PRS object, enabling an associated IadorEcordBindingPtr PICRS (PRS) between the interface and objects; // Call _Recordset Open method to get Recordset object PRS-> Open ("Select * from Employee Order By Lname", "DSN = Pubs; UID = SA; PWD =;", AdopenStatic, AdlockOptimistic, AdcmdText; // Using CCUSTOMRS classes Macro definition, the binding between data // If the bind is successful, we can use the attribute of the RS object Testhr (Picrs-> Bindtorecordset); // Everyone pays attention to the endoffile here Rename ("EOF", "endoffile") in the Import statement is actually EOF. The role of rename is to prevent naming conflicts. While (! pRS-> endoffile) {// process data in the ccustomrs C Instance Variables. Printf ("Name =% S% S / N", (rs.m_ul_fnamestatus == adfldok? rs.m_ch_fname: "" ), (Rs.m_ul_lnamestatus == adfldok? Rs.m_ch_lname: ""));

转载请注明原文地址:https://www.9cbs.com/read-1800.html

New Post(0)