VC database development

xiaoxiao2021-03-06  39

1. Overview 1,1 Visual C Develop Database Technology Visual C provides a variety of database access technology - IDBC API, MFC ODBC, DAO, OLE DB, ADO, etc. Each of these technologies has its own characteristics, providing simple, flexible, visiting development technologies, and scalable development technologies. Simplified Visual C provides a series of Wizard tools such as the MFC class, ATL template class, and AppWizard, ClassWizard to help users build their own applications, greatly simplify the design of the application. With these technologies, you can develop a small code or you can develop a database application if you write very little code or you don't need to write code. Flexibility Visual C development environment allows developers to design applications and features based on their needs, and Visual C provides a wide range of libraries and methods that allow developers to choose according to their own application features. Access Speed ​​For the Slowness of the database application for ODBC development, Visual C provides new access technology --ole DB and ADO, OLE DB and ADO are Based on COM interface, using this technology. You can directly access the driver of the database, which greatly provides access speed. Scalability Visual C provides OLE technology and ActiveX technology, which enhances the ability of applications. Using OLE technology and ActiveX technology can enable developers to create their own programs using components provided by Visual C to create their own programs to implement the components of the application. Using this technique allows the application to have good scalability. Accessing Different kinds of data sources Traditional ODBC technology can only access relational databases, in Visual C , providing OLE DB access technology, not only accessible databases, but also access non-relational databases. 1,2 Visual C Development Database Technology Visual C provides a variety of technologies for accessing the database, as shown below: ODBC (Open Database Connectivity) MFC ODBC (Microsoft Foundation Classes ODBC) DAO (Data Access Object) OLE DB (Object Link and Embedding Database) ADO (ActiveX Data Object) has its own characteristics, summarizes the following: ODBC ODBC is a unified interface provided by the client application access to the relational database. For different databases, ODBC provides a unified API, Enable the application to apply the supplied API to access any database that provides an ODBC driver. Moreover, ODBC has become a standard, so all relational databases currently provide ODBC drivers, which makes ODBC's applications very wide, basically available for all relational databases. However, since ODBC can only be used for relational databases, it is difficult to access object databases and other non-relational databases using ODBC. Since ODBC is a bottom-level access technology, ODBC APIs can enable client applications to set up and control databases from the underlying, and complete some of the high-level database technologies that cannot be completed. The MFC ODBC is used to prepare a large number of code directly using the ODBC API, and the MFC ODBC class is provided in Visual C , which encapsulates the ODBC API, which makes it easy to use MFC to create ODBC applications.

DAO DAO provides a mechanism for creation and manipulating a database via program code. Multiple DAOs constitute an architecture, in which each DAO object works. MFC DAO is a powerful database development tool provided by Microsoft Jet Database file (* .mdb), which provides a DAO rich operation database means by DAO packaging. OLE DB OLE DB is a new technology provided in Visual C development database applications, which is based on a COM interface. Therefore, OLE DB provides unified interfaces for all file systems including relational databases and non-relational databases. These features make OLE DB technology more superior to traditional database access technology. Similar to ODBC technology, OLE DB belongs to the underlying interface in database access technology. Use OLE DB to design a database application that requires a lot of code. ATL templates are provided in the VC to design OLE DB data applications and data providers. ADO ADO technology is based on OLE DB access interface. It inherits the advantages of OLE DB technology, and ADO has encapsulated the OLE DB interface, defined ADO objects, making program development simplified, ADO technology belongs to the high-rise of database access interface.

2. Using the ODBC API Microsoft Open Database Interconnect (ODBC, Open Database Connectivity) is part of the Microsoft Windows Open Services System (WOSA), which is a standard interface for database access. With this standard interface, we can do not care about the details of the specific database management system (DBMS), and as long as there is an ODBC driver of the corresponding type of database, access to the database can be implemented. The ODBC programming interface provides us with great flexibility, we can access different types of databases through this interface. Moreover, through the corresponding ODBC driver, we can easily implement the conversion between different data types. 2.1 ODBC API Overview ODBC is an application extensive database access application programming interface (API), using standard SQL (Structured Query Language) as its database access language. 2.11 Architecture ODBC structure is based on the client / server architecture, which contains the following four parts: Application: Application: Application, user application, it is responsible for the interaction between users and user interfaces Operation, and call the ODBC function to give the SQL request and extract the results and the error handling. ODBC Driver Manager: The ODBC Driver Manager loads and calls the driver for your application, which can manage multiple applications and multiple drivers at the same time. Its function is to be implemented by indirect calling functions and using dynamic link library (DLL), so it is typically included in a file called "DLL". The ODBC Driver (DRIVER) ODBC driver performs an ODBC function call, which presents the SQL request to the specified data source and returns the result to the application. The driver is also interacting with any of the necessary software layers accessing the data source, including software with the underlying network or file system interface. The data source data source consists of a data set and an environment associated with it, including operating systems, dbms, and networks (if present). ODBC solves the large-scale difference problem of network topology and host by introducing the "data source" concept, so that users see the name of the data source without having to care about other things. 2.12 Data Type ODBC Use two types of data types: SQL data types and C data types. The SQL data type is used for data sources, and the C data type is used in application code. 2.13 Handle ODBC API The means of realizing database operation is a statement, which is a powerful means. In addition to executing SQL statements and completing query operations, ODBC statements can implement most database operations. In ODBC, different handles (HANDLE), Connection, Sponder, statement, descriptor, and the like. The handle is an application variable that uses it to store some of the objects for the application's context information and applications. It is similar to the concept of Windows programming, but ODBC has more perfect handle. 1. The environment handle is the handle of the entire context in the ODBC, starting from the creation environment handle using the ODBC's handle to the end of the environments. All other handles (all joint handles and statement handles all of this application) are managed by the context in the environment handle. Environment handles can only create one in each application. 2, join handle management of all information about the coupling.

The coupling handle can be assigned multiple, which is not only legitimate and useful; but do not generate unnecessary handles to avoid waste of resources. However, different drivers supported the coupling situation, and some drivers only support one join handle in an application, and some drivers only support one statement handle. In the application, you can join or depart from the data source when any appropriate time, but do not easily establish or disengage. 3, the statement handle is the ODBC API truly important role, it is used to process the SQL statement and the directory function, each statement handle is related to one connection. When the driver receives a function call instruction from the application, the instruction contains a statement handle, the driver manager will send this function to the appropriate driver using the coupling handle stored in the statement handle. 4, the descriptor handle is a collection of metadata, which describes the parameters of the SQL statement, and the columns of the record set. After the statement is assigned memory, the descriptor is automatically generated, called an automatic allocation descriptor. In the program, the application can also call the SQLAllocHandle Assignment Descriptor. When the application calls the API function SQLALLOCHANDLE, the driver manager or the ODBC driver will assign internal structures to the declared handle type and return the handle value. 2.14 Abnormally Handling In order to debug the program during the program development process, the ODBC API returns the information about the ODBC API function in two ways: return code and diagnostic records. The return code returns the return value executed by the function, indicating that the function performs success or not. Diagnostic Record Description Function Details. Return Code Each ODBC API function returns a code-return code, indicating the success of the function execution. If the function call is successful, the return code is SQL_SUCCESS or SQL_SUCCESS_WITH_INFO. SQL_SUCCESS indicates that the detailed information about the operation can be obtained by diagnostic logging, and SQL_SUCCESS_WITH_INFO indicates that the application execution result with a warning message, and detailed information can be obtained by diagnostic logging. If the function call fails, the return code is SQL_ERROR. The following code is based on the return code executed by the function SQLFETCH (), and determines the success of the function execution, thereby performing corresponding processing 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 success, Processing} If the program performs an error, the return code is SQL_INVALID_HANDLE, and the program cannot be executed, while the other return code has the information executed by the program. Diagnostic Records Each ODBC API function can produce a series of diagnostic records that reflect operation information. These diagnostic records are placed in a related ODBC handle until the next function called using the same handle, the diagnostic record has always exists. The size of the diagnostic record is not limited. Diagnostic records have two categories: Head Record and Status Record. The header is a first copyright method (RECORD 0), and the recording is recorded as a status record. Diagnostic records have a number of domains, which are different in head recording and status records. You can use the SqlgetDiagField function to get specific domains in the diagnostic record, and SQLGETDIAGREC () can be used to obtain some common domains in diagnostic records, such as SQLSTATE, original error number, and the like.

The universal information of a function execution is included in each domain of the header record, regardless of whether the function is successful, as long as SQL_INVALID_HANDLE is not returned, the head record will be generated. Each field in the status recording status record contains a particular error or warning message returned by the drive manager, an ODBC driver, or a data source, including SQLState, original error code, diagnostic information, number, and a line number. Diagnostic records are generated when only functions return SQL_ERROR, SQL_STILL_EXEUTING, SQL_SUCCESS_WITH_INFO, SQL_NEED_DATA, or SQL_NO_DATA. Use SqlgetDiagRec and SqlgetDiagField applications to call function SQLGETDIAGREC or SQLGETDIAGFIELD to get diagnostic information. For a given handle, these two functions returns diagnostic information that recently used the function of the handle. When there is a function using the handle, the handle record is covered by the original diagnostic information recorded. If the function is executed, multiple status records are generated, and the program must call these two functions multiple times to obtain information (2) although directly applied the ODBC API program application relatively cumbersome, but because the procedures written directly using the ODBC API relative To be simple, efficient 2.2 Application ODBC API Establish an application although the ODBC API program application is relatively cumbersome, it is relatively simple, efficient because the procedures written directly using the ODBC API. So, we need to learn directly using the ODBC API program. King of viewing ACDSEE 6.0 Application Daquan Digital Portrait Photograph Vectorization and Art Treatment Six Days Six Days Visual Basic Database Programming Analysis "Sway" Malignant Worm Virus

In general, the writing ODBC program has the following steps: Assign an ODBC environment Assigning connection handle connection data source construct and executes the SQL statement to obtain the execution result to disconnect the connection of the connection ODBC environment 2.21 Assign an ODBC environment for any ODBC application Program, the first step is to load the driver manager, then initialize the ODBC environment and assign environment handles. First, a variable of a SQLhenv type is declared, then call the function sqlallochandle, transfer the above-mentioned SQLhenv type variable address and SQL_HANDLE_ENV options allocated. As shown in the following code: SQLhenv Henv; Sqlhenv Henv; SQLAllochandle (SQL_HANDLE_ENV, SQL_NULL_HANDLE, & HENV); After executing the calling statement, the driver assigns a structure, the environment information is stored, and then returns the environment handle corresponding to the environment. 2.22 Assigning the connection handle to assign the environment handle, we must assign a connection handle before establishing a connection to the data source, each of which is connected to the data source corresponding to a connection handle. First, the program defines a variable of a SQLHDBC type, which is used to store the connection handle, and then call the SQLAllocHandle function assigning the handle. As shown below: SQLHDBC HDBC; SQLAllochandle (SQL_HANDLE_DBC, HENV, & HDBC); Henv is a environment handle. 2.23 Connection Data Source When the connection handle is allocated, we can set the connection properties, all connection properties have default, but we can set the connection properties by calling the function sqlsetConnecttTR (). Use a function sqlgetconnectatTR () to get these connection properties. Functions in the following format: SQLRETURN SQLSetConnectAttr (SQLHDBC ConnectionHandle, SQLINTEGER Attribute, SQLPOINTER ValuePtr, SQLINTEGER StringLength); SQLRETURN SQLGetConnectAttr (SQLHDBC ConnectionHandle, SQLINTEGER Attribute, SQLPOINTER ValuePtr, SQLINTEGER StringLength); the application can set different connection properties according to their needs. After completing the settings for the connection properties, you can build a connection to the data source. For different programs and user interfaces, you can create connections with different functions: SqlConnect, SqlDriverConnect, SQLBROWSECONNECT. SqlConnect This function provides the most direct program control method, and we can connect to the data source name, user ID, and password.

Function Format: SQLRETURN SQLConnect (SQLHDBC ConnectionHandle, SQLCHAR ServerName, SQLSMALLINT NameLength1, SQLCHAR UserName, SQLSMALLINT NameLength2, SQLCHAR * Authentication, SQLSMALLINT NameLength3); Parameters: ConnectionHandle connection handle ServerName data source name NameLength1 data source name length UserName user ID NameLength2 user ID length password Authentication user password length NameLength3 return value:. SQL_SUCCESS, SQL_SUCCESS_WITH_INFO, SQL_ERROR, or SQL_INVALID_HANDLE successful return SQL_SUCCESS, if the return value SQL_ERROR or SQL_SUCCESS_WITH_INFO, SQLGetDiagRec function can be used to obtain the value of the corresponding SQLSTATE.