Analysis of activity data objects in Visual C ++

Source: Internet
Author: User
Tags ole
Analysis of activity data objects in Visual C ++
Program running effect:

Abstract: This article briefly introduces Microsoft's Active Data Object (ADO) model, and describes the basic steps for using ADO to manipulate databases in Visual C ++, this paper analyzes the features of ADO and the differences between ADO and Open Database Connection (ODBC) and their application prospects.

Figure left: ADO object model

Keywords: Activity data object database visual c ++

1 ADO is an integral part of Microsoft's entire com strategic system

An Active Data Object (ADO) is a group of COM components provided by Microsoft. ADO is built on the com architecture advocated by Microsoft. All its interfaces are automated interfaces, therefore, you can access ADO through interfaces in C ++, Visual Basic, Delphi, and other development languages that support COM. By using the new ole db technology, ADO allows you to access data from relational databases, text files, non-relational databases, indexing servers, and Active Directory Services in the same way, it expands the range of data sources available in applications and becomes the preferred choice for accessing data source components in Microsoft's entire com strategic system. It is an alternative to ODBC.

2 ADO object model Composition

Compared with other data access models of Microsoft, Dao and rdo, ADO object models are very refined. They only consist of three main objects: connection, command, recordset, and several secondary objects. The connection object provides the association between the ole db data source and the conversation object. It uses the user name and password to process user identity authentication and provides transaction processing support. It also provides execution methods, this simplifies the process of data source connection and data retrieval. The command object encapsulates commands that can be interpreted by the data source. This command can be anything that can be understood by SQL commands, stored procedures, or underlying data sources. Record set is used to indicate the table data returned from the data source. It encapsulates methods such as navigation, Record Update, record deletion, and adding new records of the record set, and also provides the ability to update records in batches. Other secondary objects provide columns that encapsulate ADO errors, encapsulation command parameters, and encapsulation record sets.

3. Analysis of ADO features

(1) because it encapsulates a lot of underlying work, using ADO is almost as convenient as using ODBC.

(2) ado not only has the main functions of ODBC, but also applies to a wide range of data sources.

(3) when defining ADO record set variables and database table fields to bind classes, the number and sequence of field variables and state variables of the record set must be the same as those of the database table fields. This is more complicated than using ODBC in FMC. However, in the macro that the database field is bound to the ADO record set field variable, ADO provides much more data types than rfx in FMC (such as date and time type, can only be converted to cstring type in ODBC ).

(4) ADO allows multiple record set instances under the same connection instance.

(5) ADO allows batch Update (using the update batch method), which will greatly reduce the network burden and improve the database processing efficiency.

4. Use ADO in Visual C ++

Using the ado2 provided by Microsoft in Micrsoft Studio 6, you can use the ADO interface in Visual C ++ to manipulate the SQL Server database. Using ADO in compiled advanced languages is more difficult than using ADO in some scripting languages such as Visual Basic scropt and JavaScript.

The following describes the basic steps for using the connection object and record set object of ADO in Visual C ++:

(1) Use the import command to introduce the ado2 component

Example: # import "C: adomsado15.dll" no_namespace Rename ("EOF", "endoffile ")

(2) define the derived class of cadorecordbinding for interaction between programs and database table fields. For the definition of this class, see icrsint. h.

Example:

Class cintlive: Public cadorecordbinding
{
Public:
Dbtimestamp m_datetime; // defines the field variable of the ADO record set (corresponding to the field of the database table)
Long m_key;
Long m_value;
Long m_quality;
Word m_stsdatetime; // defines the state variable of the ADO record set.
Word m_stskey;
Word m_stsvalue;
Word m_stsquality;
Begin_ado_binding (cintlive) // bind the database field to the ADO record set field variable
Ado_variable_length_entry2 (1, addbtimestamp, m_datetime, sizeof (m_datetime), m_stsdatetime, true)
Ado_numeric_entry (2, adinteger, m_key, 10, 0, m_stskey, true)
Ado_numeric_entry (3, adinteger, m_value, 10, 0, m_stsvalue, true)
Ado_numeric_entry (4, adinteger, m_quality, 10, 0, m_stsquality, true)
End_ado_binding ()
};

(3) Call coinitialize to initialize COM: coinitialize (null );

(4) declare and initialize the connection object pointer and recordset Object Pointer of ADO. (The type name is defined in msado15.dll)

Example:

_ Connectionptr pconnection1 = NULL;
_ Recordsetptr rstado1 = NULL;

(5) define the instance of the cadorecordbinding derived class and its bind interface pointer.

Example:

Cintlive m_intdata;
Iadorecordbinding * rstadobind1 = NULL;

(6) generate a connection object instance and a record set object instance.

Example:

Pconnection1.createinstance (_ uuidof (connection ));
Rstado1.createinstance (_ uuidof (recordset ));

(7) connect to the database and open the record set object. For the usage of open function parameters, see the basic description of ADO object parameters in Microsoft msdn.

Example:

Pconnection1-> open ("driver = {SQL Server}; server = servera; uid = sa; Pwd =; database = pubs", "", "", null );
Rstado1-> open ("data", _ variant_t (idispatch *) pconnection1, true ),
Adopenkeyset, adlockbatchoptimistic, adcmdtable );

(8) associate the instance of the cadorecordbinding derived class with the BIND interface of the record set object.

Example:

Rstadobind1-> bindtorecordset (& m_intdata );

(9) operate on the record set object instance. For the operation method, see the basic description of the corresponding method of ADO record set object in Microsoft msdn.

Example:

Rstado1-> move next (); // move the cursor to the next record
Rstado1-> Update (_ variant_t ("quality"), _ variant_t ("3"); // modify the record's quality field value to 3
Rstado1-> Update batch (adaffectall); // sends all updates on the record set object to the database once.

(10) Close the record set object and release the BIND interface.

Example:

Rstado1-> close ();
Rstadobind2-> release ();

(11) Close the connection pconnection1-> close ();

(12) Call counitialize to release com resources: couninitialize ();

5 conclusion

As an alternative to ODBC, ADO does have extraordinary advantages. Since the ADO data source almost covers the common data source types, ADO is undoubtedly the only choice for data sources not supported by ODBC. The batch update function of ADO is an important factor for updating applications with large data volumes in the network environment. Due to the lack of support from a large number of third-party vendors, ADO is far less popular than ODBC, but its object-oriented feature will make ADO have a broad development prospect.

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.