VC database programming Overview

Source: Internet
Author: User

I. database access technology provided by VC

Odbc api, mfc odbc, Dao, ole db, and ADO

Access speed: Traditional ODBC is slow. Both ole db and ADO are based on the COM technology. With this technology, you can directly access the database driver.ProgramThe speed is greatly improved.

 

Scalability: Using ole db and ActiveX technology, you can use various components, controls, and third-party components provided by VC. To implement application componentization.

 

Access different data sources: Traditional ODBC can only access relational databases. The old DB technology provided in VC can solve this problem.

Ii. Technical Overview

1. ODBC API

Provides a common interface that provides drivers for both Oracle and SQL Server. You can directly perform underlying function operations on the database using SQL statements. Use the following header files. "SQL. H", "sqlext. H", "sqltypes. h ".

The procedure is as follows:

Step 1: allocate the ODBC environment and initialize some internal structures. To complete this step, you need to assign a sqlhenv type variable to use as a handle in the ODBC environment.

Step 2: assign a connection handle to each data source to be used. The sqlallochandle () function is complete.

Step 3: Use sqlconnect () to connect the connection handle to the database. You can first set the connection attribute through sqlsetconnectattr.

Step 4: perform SQL statement operations. After the operation, you can disconnect the database.

Step 5: Release the ODBC environment.

 

Features: powerful functions, advanced functions such as asynchronous operations and transaction processing. Currently, all relational databases provide ODBC drivers, which are widely used.

Disadvantage: it is difficult to access the object database and non-relational database.

 

2. MFC ODBC

 

To simplify the use of ODBC APIs, VC provides the mfc odbc class and encapsulates ODBC APIs.

The main types of mfc odbc are as follows:

Cdatabase class: A cdatabase object represents a connection to the data source, through which the data source can be operated. Generally, you do not need to directly use the cdatabase object because the cdecordset object can implement most functions. However, cdatabase plays a key role in transaction processing.

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 a static view. The latter indicates that the record set is synchronized with other users for database updates.

Crecordview class: displays the view of database records in a space. This view is directly connected to a format view of a crecordset object. It creates a template resource from a dialog box. The fields of the crecordset object are displayed in the control of the dialog box template. The object uses the DDX and rfx mechanisms. Automate the movement of data between the control in the format and the fields in the record set.

Cdbexception class: Derived from the cexception class, which reflects database operation exceptions with three inherited variables.

M_nretcode: ODBC return code.

M_strerror: string to describe the cause of an error thrown.

M_strstatenativeorigin: string that describes the Exception error indicated by an ODBC error code.

 

3. MFC DAO

 

Mfc dao is provided by Microsoft to access Microsoft Jet Database Files (*. MDB. This technology is convenient when you only need to access the ACCESS database.

4. OLE DB

 

Similar to ODBC technology, ole db is the underlying interface in database access technology.

Using oledb directly requires a large numberCode, VC provides the ATL template,

The oledb Framework defines three basic classes of applications.

 

Data Provider: an application that has its own data and displays data in tables.

User consumers: Use the oledb interface to control the data stored in the data provider.

Serviceprovider: A combination of data providers and users.

 

When Programming Using ole db, you can use component objects to develop programs. 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. Ado

 

ADO is an ole db-based access interface. It inherits the advantages of oledb, encapsulates oledb interfaces, defines ADO objects, and simplifies development. ADO is a high-level interface for database access.

 

ADO is very effective in server applications, especially the support for Dynamic Server Pages (ASP.

 

The ADO object structure is similar to oledb, but does not rely on Object layers. In most cases, you only need to create and use the required objects. The following object classes constitute the ADO interface.

 

Connection: used to indicate the connection to the database and process some transactions and commands.

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: transmits data between commands sent to the data source.

Property: The detailed properties of other objects used in ADO.

Errro: used to obtain detailed error information that may occur.

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.