How to allocate ODBC handles

The handle types required for ODBC programs are as follows:

Item Handle type
Environment SQLHENV
Connection SQLHDBC
Statement SQLHSTMT
Descriptor SQLHDESC

To use an ODBC handle, you perform the following tasks:

  1. Call the SQLAllocHandle function.

  2. Use the handle in subsequent function calls.

  3. Free the object using SQLFreeHandle.

SQLAllocHandle takes the following parameters:

SQLFreeHandle takes the following parameters:

Example

The following code fragment allocates and frees an environment handle:

SQLRETURN rc;
SQLHENV env;
rc = SQLAllocHandle( SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env ); 
if( rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO ) 
{
    .
    .
    .
}
SQLFreeHandle( SQL_HANDLE_ENV, env );