Use Visual C ++ to develop database applications (1)

Source: Internet
Author: User
Tags driver manager
Use Visual C ++ to develop database applications (1) 1. Overview 1. 1. Features of Visual C ++ database development Visual C ++ provides a variety of Database Access Technologies-odbc api, mfc odbc, DAO, ole db, and ADO. These technologies have their own characteristics. They provide simple, flexible, fast access, and scalable development technologies. Simple Visual C ++ provides the MFC class library, ATL template class, AppWizard, ClassWizard, and a series of other Wizard tools to help users quickly build their own applications, this greatly simplifies the design of applications. By using these technologies, developers can develop a database application with little or no need to write code. The flexible development environment provided by Visual C ++ allows developers to design application interfaces and functions as needed. In addition, Visual C ++ provides a wide range of class libraries and methods, developers can choose based on their application characteristics. Fast access speed to solve the problem of slow database access speed for database applications developed by ODBC, Visual C ++ provides a new access technology-ole db and ADO, ole db and ADO are both based on the COM interface technology. Using this technology, you can directly access the database driver, which greatly provides access speed. Extensibility Visual C ++ provides OLE and ActiveX technologies, which can enhance application capabilities. With OLE and ActiveX technologies, developers can use various components and controls provided by Visual C ++, as well as components provided by third-party developers to create their own programs, so as to implement componentization of applications. Using this technology can make applications highly scalable. Traditional ODBC technology for accessing different types of data sources can only access relational databases. in Visual C ++, ole db access technology is provided to access not only relational databases, but also non-relational databases. 1. 2 Visual C ++ development DataBase technology Visual C ++ provides a variety of DataBase Access Technologies, as shown below: ODBC (Open DataBase Connectivity) mfc odbc (Microsoft Foundation Classes ODBC) the DAO (Data Access Object) ole db (Object Link and Embedding DataBase) ADO (ActiveX Data Object) technologies have their own characteristics, which are summarized as follows: ODBC is a unified interface provided by your applications to access relational databases. For different databases, ODBC provides a unified set of APIs, allows applications to access any database that provides the ODBC driver by using the APIS provided by the application. In addition, ODBC has become a standard. Therefore, all relational databases currently provide ODBC drivers, which make ODBC widely used and can be basically used in all relational databases. However, because ODBC can only be used for relational databases, it is difficult to use ODBC to access object databases and other non-relational databases. Because ODBC is an underlying access technology, ODBC APIs enable customer applications to set and control databases from the underlying layer to complete functions that cannot be completed by some advanced database technologies. Due to the fact that you need to compile a large amount of code to directly write an application using the odbc api, the mfc odbc class is provided in Visual C ++ and the odbc api is encapsulated, this makes it easy to use MFC to create ODBC applications. DAO provides a mechanism for creating and manipulating databases through program code. Multiple DAO forms an architecture in which each DAO object works collaboratively. Mfc dao is provided by Microsoft to access Microsoft Jet Database Files (*. mdb) is a powerful database development tool, which provides programmers with a variety of DAO database operations through DAO encapsulation. Ole db is a new technology provided by Visual C ++ in developing database applications. It is based on the COM interface. Therefore, ole db provides a unified interface for all file systems, including relational databases and non-relational databases. These features make ole db technology superior to traditional database access technology. Similar to ODBC technology, ole db is the underlying interface in database access technology. Using ole db directly to design database applications requires a lot of code. An ATL template is provided in VC for designing ole db data applications and data providers. ADO technology is an access interface based on ole db. It inherits the advantages of ole db technology. In addition, ADO encapsulates ole db interfaces and defines ADO objects, simplify program development. ADO technology is a high-level interface for database access. 2. Using odbc api Microsoft Open DataBase Connectivity (ODBC, Open DataBase Connectivity) is part of Microsoft Windows Open service system (WOSA) and is a standard interface for DataBase access. With this standard interface, we can ignore the details of the specific database management system (DBMS), and access to the database can be implemented as long as there is an ODBC driver for the corresponding type of database. The ODBC programming interface provides us with great flexibility. We can use this interface to access different types of databases. In addition, through the corresponding ODBC driver, we can easily achieve conversion between different data types. 2.1 odbc api overview ODBC is a widely used programming interface (API) for accessing databases. It uses standard SQL (structured query language) as its database access language. 2.11 architecture ODBC is built on the client/server architecture. It consists of the following four parts: Application, that is, the user's Application, it is responsible for interaction between users and user interfaces, and calls odbc functions to provide SQL requests, extract results, and handle errors. ODBC Driver Manager: the ODBC Driver Manager loads and calls drivers for applications. It can manage multiple applications and drivers at the same time. Its function is implemented by indirectly calling a function and using a dynamic link library (DLL). Therefore, it is generally included in a file with the extension "DLL. ODBC Driver ODBC drivers execute ODBC function calls, send SQL requests to the specified data source, and return the results to the application. Drivers are also responsible for interacting with any necessary software layer that accesses the data source, including software that interfaces with the underlying network or file system. A data source consists of a dataset and an associated environment, including the operating system, DBMS, and network (if any ). ODBC solves the wide range difference between the network topology and the host by introducing the "Data Source" concept. In this way, users can see the name of the data source without worrying about anything else. 2.12 data type ODBC uses two types of data: SQL data type and C data type. The SQL data type is used for data sources, and the C data type is used in application code. 2.13 using the odbc api to perform database operations is a statement, which is a powerful means. In addition to SQL statements and query operations, ODBC statements can also perform most database operations. In ODBC, different handles are used to indicate ENVIRONMENT (ENVIRONMENT), CONNECTION (CONNECTION), STATEMENT (STATEMENT), and DESCRIPTOR. A handle is an application variable. The system uses it to store context information about the application and some objects used by the application. It is similar to the concept in Windows programming, but ODBC is more perfect for handling. 1. The environment handle is the entire context handle in ODBC. Each program in ODBC starts from the creation of the environment handle and ends with the release of the Environment handle. All other handles (all the connection handles and statement handles of the application) are managed by context in the Environment handle. Only one environment handle can be created in each application. 2. The connection handle manages all information about the connection. Multiple join handles can be allocated, which is not only legal but also useful. do not generate unnecessary handles to avoid resource waste. However, different drivers support different connections. Some drivers only support one connection handle in an application, and some drivers only support one statement handle. In an application, you can connect to or remove from the data source whenever appropriate, but do not create or detach the connection easily. 3. The statement handle plays an important role in ODBC APIs. It is used to process SQL statements and directory functions. Each statement handle is related to only one join. When the driver receives a function call instruction from the application and the instruction contains a statement handle, the driver Manager uses the connection handle stored in the statement handle to send this function call to the appropriate driver. 4. the descriptor handle is a collection of metadata, which describes the SQL statement parameters, record set columns, and other information. When a statement is allocated with memory, the descriptor is automatically generated. In the program, the application can also use SQLAllocHandle to allocate descriptors. When an application calls the API function SQLAllocHandle, the driver manager or ODBC driver allocates an internal structure for the declared handle type and returns the handle value. 2.14 Exception Handling in order to debug the program during program development and discover program errors, odbc api returns information about odbc api function execution in two ways: return code and diagnosis record. The return code returns the return value of the function execution, indicating whether the function execution is successful or not. Diagnostic Records describe detailed information about function execution. Each odbc api function returns a Code-Return Code, indicating whether the function is successfully executed. If the function is successfully called, the return code is SQL _SUCCESS or SQL _SUCCESS_WITH_INFO. SQL _SUCCESS indicates that detailed information about the operation can be obtained through the diagnosis record, SQL _SUCCESS_WITH_INFO indicates that the application execution result contains warning information, and detailed information can be obtained through the diagnosis record. If the function call fails, the return code is SQL _ERROR. The following code checks whether the function is successfully executed based on the return code of SQLFetch (), and then processes the Code accordingly. SQLRETURN rtcode; SQLHSTMT hstmt; While (rtcode = SQLFetch (hstmt )! = SQL _NO_DATA) {if (rtcode = SQL _SUCCESS_WITH_INFO) {// display warning information} else {// Display Error information break;} // function call successful, processing} if a program execution error occurs, the return code is SQL _INVALID_HANDLE, and the program cannot be executed. Other return codes contain information about program execution. Diagnostic Records (Diagnostic Records) Each odbc api function can generate a series of Diagnostic Records that reflect the operation information. These diagnostic records are stored in the relevant ODBC handle until the next function call using the same handle exists. There is no limit on the size of diagnostic records. There are two types of diagnostic records: Head Record and Status Record ). The header Record is the first copyright law Record 0, and the subsequent Record is a status Record. Diagnostic records are composed of many domains, which are different in the header record and status record. You can use the SQLGetDiagField function to obtain specific fields in the diagnosis record. In addition, you can use SQLGetDiagRec () to obtain some common fields in the diagnosis record, such as SQLSTATE and original error numbers. Each field in the header record contains a common information about function execution. A header record is generated no matter whether the function is successfully executed or not, as long as SQL _INVALID_HANDLE is not returned. Each domain in the status record contains specific error or warning information returned by the driver manager, ODBC driver, or data source, including SQLSTATE, original error code, diagnostic information, column number, and row number. Diagnostic records are generated only when SQL _ERROR, SQL _STILL_EXEUTING, SQL _SUCCESS_WITH_INFO, SQL _NEED_DATA, or SQL _NO_DATA is returned by function execution. The SQLGetDiagRec and SQLGetDiagField applications can call the SQLGetDiagRec or SQLGetDiagField functions to obtain diagnostic information. For a given handle, these two functions return the diagnostic information of the function that recently used the handle. When a function execution using the handle is performed, the original diagnostic information recorded by the handle is overwritten. If multiple state records are generated after the function is executed, the program must call these two functions multiple times to obtain information.

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.