Allocating an ODBC Handle

Use SQLAllocHandle and SQLFreeHandle functions to allocate ODBC handles.

  1. Call the SQLAllocHandle function, which takes these parameters:
    • An identifier for the type of item being allocated

    • The handle of the parent item

    • A pointer to the location of the handle to be allocated

    For a full description, see SQLAllocHandle in the Microsoft ODBC Programmer's Reference.

  2. Use the handle in subsequent function calls.
  3. Free the object using SQLFreeHandle, which takes these parameters:
    • An identifier for the type of item being freed

    • The handle of the item being freed

    For a full description, see SQLFreeHandle in the Microsoft ODBC Programmer's Reference.

    For example, this code fragment allocates and frees an environment handle:

    SQLHENV env;
    SQLRETURN retcode;
    retcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env );
    if ( retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO )
    {
    // success: application code here
    }
    retcode = SQLFreeHandle(SQL_HANDLE_ENV, env);