Comparison of several VC database development technologies
Some programming interfaces are available from a functionally simple database (such as Jet Engine) to complex large database systems (such as Oracle). This article mainly introduces the following five types:
1.ODBC API; 2.MFC ODBC class; 3.mfc DAO class; (Data Access Object) 4. MFC OLE / DB; 5.activeX Data Object (ADO).
1. Open Database Connection (ODBC API): Provides a universal programming interface that allows programs to connect to a variety of different databases. It provides drivers for Oracle, SQL Server, MS Excel, etc., allowing users to use SQL statements to perform direct underlying functional operations on the database. When using the ODBC API, the header of the user must introduce is "sql.h", "sqlext.h", "sqltypes.h". Creating a database application with an ODBC API follows a certain basic steps:
The first step is to assign an ODBC environment to initialize some internal structures. Complete this step, you must allocate a SQLHENV type variable to perform handle in the ODBC environment.
The second step is to assign a connection handle to each of the data sources to be used, and is done by the function sqlallochandle ().
The third step is to connect the connection handle to the database using SqlConnect (), you can set the connection properties through SQLSetConnectatTR ().
Then you can perform the operation of the SQL statement, limited to the space, the relevant function is not specifically introduced, the reader can refer to the relevant books.
Once the operation is completed, the user retrieves the corresponding result, you can cancel the connection to the database.
Finally, you need to release the ODBC environment.
The ODBC API is characterized by strong function, providing advanced features such as asynchronous operation, transaction processing, but the corresponding programming is complicated, and the workload is large.
2. MFC ODBC class: The version of the ODBC function is introduced in the version after the MFC1.5. By providing the interface with the ODBC through these classes, the user can perform database operations without having to process complicated processing in the ODBC API. The main MFC ODBC classes are as follows.
CDATABASE class: A CDatabase object represents a connection to the data source, which can operate the data source. The application can use multiple CDatabase objects: construct an object and call the OpenEx () member function to open a connection. The CRecordset object is then constructed to operate the connected data source, and pass the record set constructor pointer to the CDATABASE object. After using the Close () member function, use the close () member function. Under normal circumstances, it is not necessary to use the CDATABASE object directly because the CRecordset object can achieve most of the functions. However, CDATABASE plays a key role in conducting transaction. Transaction refers to the update of a series of data sources, and is submitted or not submitted, which ensures that multi-user data is correct when the data source is operated simultaneously.
CRecordset class: A CRecordset object represents a collection of records from a set of records selected from the data source. There are two forms of record sets: Snapshot and Dynaset. The former represents the static view of the data, the latter means that the record set is synchronized with other users' updates to the database. With CRecordset objects, users can perform various operations in the database.
CRecordView class: CRecordView object is a view that displays database records in space. This view is a format view directly connected to a CRecordset object, created from a dialog template resource, and displays the field of the CRecordset object in the control of the dialog template. Objects utilize DDX and RFX mechanisms, so that data moves between controls and records in the format, that is, users do not even write a line of code to implement a simple database record view program. CDBEXCEPTION Class: Depends by the CEXCeption class, with three inherited member variables reflect the exception of the database operation: m_nretcode: The form of an ODBC returns code (SQL_RETURN) indicates the cause of abnormalities. M_Strerror: String, description causes an error reason that throws an exception. M_STRSTATINATIVEORIGIN: String to describe an exception error indicated by ODBC error code.
The MFC database class member function can throw an exception of the CDBException type, so monitoring an exception after the code is operated.
The MFC ODBC class is the most widely used in actual development because it is rich in functionality and is relatively simple.
3. MFC DAO (Data Access Object) Programming: DAO is used to connect with Microsoft Access database interfaces. If you use DAO programming if you only need to interface with the Access database interface. Its main classes are as follows.
CDAOWORKSPACE: CDAOWORKSPACE object allows a user to manage the database session process of the specified password protected from logging in to the departure. In most cases, don't create a certain work area object without multiple workspaces. Because when the database and record set objects are turned on, they can use the DAO default workspace.
CDAODATABASE: represents a connection, similar to the above CDatabase class. CDAORECORDSET: Used to select a recordset and operate, similar to the above CRecordset class. CDAORECORDVIEW: Similar to the above CRecordView class. CDAOEXCEPTION: Similar to the above CDBException class. CDaOTableDef: Represents the definition of the basic table or add-on table. Each DAO database object includes a collection called TableDef that contains all stored DAO table definition objects. The CDaOTableDef object can be used to control the table definition. CDAOQUERYDEF: The CDAOQueryDef object represents a query definition (QueryDef). CDAOFIELDEXCHANGE: Supports the DAO Field Exchange (DFX) routine used by the database class. Transactions can also be handled, similar to the MFC ODBC class. The MFC DAO is only used to support Access databases, the application range is relatively fixed.
4.ole DB: OLE DB provides a flexible component object model (COM) interface between data providers and users, which sometimes makes operations complicated. The OLE DB framework defines three basic classes of the application.
Data Provider Data Provider: It has its own data and displays the application of data in a table. Provides the rowset COM interface of OLE DB, and the display range can know more complex distributed database systems from a simple provider of a single data table.
Users Consumers: Applications that control the data stored in the data provider using the OLE DB interface. The user application is classified as the class.
Service Provider Service Provider: is a combination of data providers and users. Service provider does not have its own data, but use
The OLE DB user interface to access data stored in the data provider. Then, the service provider makes the data to be valid for the user by opening the data provider interface. The service provider is often used to provide high-level services to the application, such as advanced distributed queries. When OLE DB is programmed, the user uses component object development applications. These components include: Enumerator: Used to list available data sources; data sources: represents separate data and service providers for creating a dialogue; dialogue: For transactions and commands; transactions: used to use multiple The operation is merged into a single transaction; command: to send a text command (SQL) to the data source, return to the routine; error: Used to get the error message.
5.ActiveX Data Object (ADO): It is the object-oriented interface provided by Microsoft, similar to OLE DB, but the interface is simpler, a broader feature array and higher degree of flexibility. ADO is based on COM, providing programming language available, in addition to VC , also provides applications for other development tools, such as VB, VJ, etc. ADO is very useful in server applications, especially for dynamic server page ASP (Active Server Page).
The ADO object structure is similar to OLE DB, but does not rely on the object level. In most cases, users only need to create and use only objects that need to be processed. The following object class consists of an ADO interface.
Connection: It is used to represent a connection to the database, and process some commands and transactions. Command: The command used to handle the transfer to the data source. Recordset: Table set used to process data, including get and modify data. Field: Used to indicate columns in the record set, including column values and other information. Parameter: The data is transferred back and forth between the commands transmitted to the data source. Property: Detailed properties with other objects used to operate in ADO. Error: Details for obtaining possible errors.
In VC uses ADO, you need to perform COM operation, and details will not be described in detail. In today's popular distributed development environment, VC 6.0 has a strong advantage in database development, and learning different technologies in different occasions, which is necessary for developers.