Note: This article is mainly from the "Database Tutorial" electronic industry Press, Shi Bole and other authoring. Sinble Summary
Handles are tools for applications and data source connections. Depending on the application's request, the driver manager allocates a piece of storage space for each driver to store information about the ODBC environment, each connection, and each SQL statement, and returns the handles representing those storage spaces to the application, which the application uses when invoking the ODBC function.
ODBC uses three handles: an environment handle, a connection handle, and a statement handle.
One of the environment handles can be connected to multiple connection handles, and a connection handle can be connected to multiple statement handles. (My words: With an already assigned environment handle, you can create a connection handle that is connected to it, and you can create a statement handle that is connected to it using the already allocated connection handle.) )
The environment handle defines a database environment, the connection handle defines a database connection, and the statement handle defines an SQL statement.
For a handle, there are two ODBC functions: the allocation function and the deallocation function.
When the allocation function is executed, the memory is allocated first, then the desired data structure is defined, and the handle to the development structure is assigned a value. Once the handle is assigned, the application can pass the handle to the used interface function, indicating the environment, connection, or handle that the function is acting on.
When the deallocation function is executed, the various handles and the memory allocated by each handle are freed.
Environment handle: henv type (mnemonic: Handle environment)
Allocation function: SQLAllocEnv (PHENV);//returns SQL_SUCCESS for Success, Sql_error indicates failure;
Release function: Sqlfreeenv (henv);
Connection handle: HDBC type (mnemonic: Handle database connection)
Allocation function: SQLAllocConnect (henv, PHDBC);//Assign a connection handle with an already assigned environment handle
Release function: SQLFreeConnect (HDBC);
Statement handle: HSTMT type (mnemonic: Handle??? )
Allocation function: sqlallocstmt (HDBC, phstmt);//Assigning a statement handle with an already allocated connection handle
Release function: SQLFreeStmt (hstmt, foption);
The foption has four values that specify which resources to release the handle
Sql_close: Used to undo all outstanding operation results and close any cursor associated with the handle.
Sql_drop: Used to release all resources related to the handle, and if the execution succeeds, the handle is no longer valid.
Sql_unbind: The column that is used to release all the binders.
Sql_reset_params: The parameter that is used to release all the binders.
The allocation and deallocation functions of the above three handles are declared in the Sql.h header file. When needed, the header file must be include.
The basic process of an ODBC application is divided into three parts: Initialize-->SQL processing--and terminate.
The order in which initialization is initiated:
1. Assigning an environment handle: sqlallocenv function
2. Assigning connection handles: SQLAllocConnect function
3. Connect to server: SQLConnect function
4. Assigning a statement handle: sqlallocstmt function
The functions involved in SQL processing are:
1.SQLExecDirect function: Execute function directly
2.SQLPrepare function: statement prep function
3.SQLExecute function: statement execution function
The order of termination is:
1. Disconnect from server: SQLDisconnect function
2. Release statement handle: sqlfreestmt function
3. Release the connection handle: SQLFreeConnect function
4. Release the environment handle: sqlfreeenv function
Where the connection database function SQLConnect is described below:
SQLConnect (HDBC, Szdsn, Cbdsn, Szuid, Cbuid, Szauthstr, CBAUTHSTR);
HDBC is an already assigned connection assignment;
Szdsn and CBDSN are the data source names and their lengths to be connected by the system;
Szuid and Cbuid represent user identifiers and their lengths;
Szauthstr and CBAUTHSTR are permission strings and their lengths
Function: SQLDisconnect (HDBC);
The SQL statement executes the relevant function description:
Direct execution function: Sqlexecudirect (hstmt, Szsqlstr, CBSQLSTR);
HSTMT is a valid statement handle
Szsqlstr and CBSQLSTR represent the SQL statement to execute and its length
Ready to execute SQL statement functions: First execute the SQLPrepare function to prepare for a sequence of SQL statements, and then call the SQLExecute function to execute.
SQLPrepare (hstmt, Szsqlstr, CBSQLSTR);//parameter meaning is the same as Sqlexecudirect
SQLExecute (hstmt);//parameter meaning same as sqlexecudirect parameter with same name
The query returns a rowset that is controlled by the cursor.
Query result get: SQLFetch move cursor, SQLGetData read cursor points to a column of values in the row.
SQLFetch (hstmt);//move the cursor to the next line
SQLGetData (hstmt, Icol, Fctype, Rgbvalue, Cbvaluemax, pcbvalue);
HSTMT as a valid statement handle
Icol and Fctype are the column numbers and types of the resulting data, respectively
Rgbvalue and Cbvaluemax are data store pointers and maximum lengths, respectively
Pcbvalue is the total number of bytes that can be returned in Rgbvalue before this call
Summary of ODBC Connection database