From simple databases (such as jet engine) to complex large database systems (such as Oracle), VC ++ 6.0 provides some programming interfaces. This article mainly introduces the following five types:
1. odbc api;
2. mfc odbc class;
3. mfc dao class; (Data Access Object)
4. Ole/DB of MFC;
5. ActiveX Data Object (ADO ).
1. Open Database Connection (odbc api): provides a common programming interface that allowsProgramIt is connected to a variety of different databases. It provides drivers for Oracle, SQL Server, MS Excel and so on, so that users can use SQL statements to directly operate the underlying functions of the database. When using ODBC APIs, you must introduce the header files "SQL. H", "sqlext. H", "sqltypes. H ". Follow these steps to create a database application using ODBC APIs:
The first step is to allocate an ODBC environment to initialize some internal structures. To complete this step, you must allocate a sqlhenv type variable for use in the ODBC environment as a handle.
The second step is to assign a connection handle to each data source to be used, which is completed by the sqlallochandle () function.
The third step is to use sqlconnect () to connect the connection handle to the database. You can first set the connection attribute through sqlsetconnectattr.
Then you can perform SQL statement operations. Due to space limitations, the relevant functions are not described in detail. Readers can refer to relevant books.
After the operation is complete, the user can retrieve the corresponding results and cancel the connection to the database.
Finally, you need to release the ODBC environment.
ODBC APIs are characterized by powerful functions and advanced functions such as asynchronous operations and transaction processing. However, the corresponding programming is complex and requires a large workload.
2. mfc odbc class: classes that encapsulate ODBC functions are introduced in Versions later than mfc1.5. These classes provide interfaces with ODBC, allowing you to perform database operations without having to handle the complicated processing in ODBC APIs. The main mfc odbc classes are as follows.
Cdatabase class: A cdatabase object represents a connection to the data source, through which the data source can be operated. Applications can use multiple cdatabase objects: Construct an object and call the openex () member function to open a connection. Then construct the crecordset object to operate the connected data source, and pass the record set constructor pointer to the cdatabase object. Use the close () member function to destroy the cdatabase object. Generally, you do not need to directly use the cdatabase object because the crecordset object can implement most of the functions. However, cdatabase plays a key role in transaction processing. A transaction refers to a series of updates to the data source, which are submitted at the same time or not submitted at the same time to ensure that multiple users operate on the data source at the same time.
Crecordset class: A crecordset object represents a set of records selected from the data source-record set. The record set has two forms: snapshot and dynaset. The former indicates the static view of data, and the latter indicates that the record set is synchronized with other users to update the database. With the crecordset object, you can perform various operations on records in the database.
Crecordview class: The crecordview object is a view that displays database records in a space. This view is a format view directly connected to a crecordset object. It creates a template resource from a dialog box and displays the fields of the crecordset object in the control of the dialog box template. Objects use the DDX and rfx mechanisms to automate the movement of data between the control in the format and the fields in the record set. That is to say, users do not even write a row.CodeYou can simply view database records.
Cdbexception class: Derived from the cexception class, which reflects database operation exceptions with three inherited member variables:
M_nretcode: indicates the cause of the exception in the form of ODBC return code (SQL _return.
M_strerror: string to describe the cause of an error thrown.
M_strstatenativeorigin: string used to describe the Exception error represented by ODBC error code.
All MFC Database member functions can throw exceptions of the cdbexception type. Therefore, it is correct to monitor exceptions after the code operates on the database.
The mfc odbc class is the most widely used in actual development because of its rich functions and ease of operation.
3. mfc dao (Data Access Object) programming: Dao is used for Access Database interfaces with Microsoft. If the database application only needs to interface with the Access database, Dao programming is more convenient. Its main classes are as follows.
Cdaoworkspace: The cdaoworkspace object allows a user to manage the entire process of a password-protected database Session from login to departure. In most cases, do not create multiple workspaces or clear workspace objects. Because when opening database and record set objects, they can use the DaO default workspace.
Cdaodatabase: represents a connection, similar to the preceding cdatabase class.
Cdaorecordset: used to select and operate a record set, similar to the preceding crecordset class.
Cdaorecordview: similar to the preceding crecordview class.
Cdaoexception: similar to the above cdbexception class.
Cdaotabledef: defines a basic table or an additional table. Each DAO database object includes a collection called tabledef, which contains all the DaO table definition objects stored. The cdaotabledef object can be used to control table definitions.
Cdaoquerydef: The cdaoquerydef object represents a query definition (querydef ).
Cdaofieldexchange: supports Dao field exchange (DFX) routines used by the database class. It can also process transactions, similar to the mfc odbc class.
Mfc dao is only used to support access databases, and its application scope is relatively fixed.
4. ole db: ole db provides flexible Component Object Model (COM) interfaces between the data provider and the user, which sometimes complicate operations. The ole db Framework defines three basic classes of applications.
Data Provider: an application that has its own data and displays data in tables. The row set COM interface of ole db is provided. The period display range can be used to know more complex distributed database systems from simple providers of a single data table.
User consumers: the application that uses the ole db interface to control the data stored in the data provider. User applications are classified as usage classes.
Service provider: A combination of data providers and users. The service provider does not have its own data, but uses
Ole db user interface to access data stored in the data provider. Then, the service provider opens the data provider interface to make the data valid for the user. Service providers are often used to provide high-level services to applications, such as advanced distributed queries.
During ole db programming, you can use component objects to develop applications. These components include:
Enumerator: Used to list available data sources;
Data source: represents individual data and service providers for creating dialogs;
Dialog: used to create transactions and commands;
Transaction: Used to merge multiple operations into a single transaction;
Command: used to send a text command (SQL) to the data source and return the row set;
Error: used to obtain error information.
5. ActiveX Data Object (ADO): It is an object-oriented interface provided by Microsoft. It is similar to ole db, but the interface is simpler, with a wider array of features and higher flexibility. ADO is based on COM and providesProgramming LanguageThe available objects are not only for VC ++, but also for other development tools, such as VB and vj. ADO is very useful in server applications, especially for Dynamic Server Pages (ASP ).
The ADO object structure is similar to ole db, but does not rely on Object layers. In most cases, you only need to create and use the objects to be processed. The following object classes constitute the ADO interface.
Connection: used to indicate the connection to the database and process some commands and transactions.
Command: The command used to process the data sent to the data source.
Recordset: The table set used to process data, including obtaining and modifying data.
Field: indicates the column information in the record set, including the column value and other information.
Parameter: used to send data back and forth between commands sent to the data source.
Property: Detailed properties of other objects used in ADO.
Error: used to obtain detailed information about possible errors.
Com operations are required to use ADO in VC ++. The detailed method will not be described here.
In today's popular distributed development environment, VC ++ 6.0 has a strong advantage in database development.
It is necessary for developers to use different technologies in different scenarios.