ODBC handles

ODBC applications use a small set of handles to define basic features such as database connections and SQL statements. A handle is a 32-bit value.

The following handles are used in essentially all ODBC applications:

  • Environment   The environment handle provides a global context in which to access data. Every ODBC application must allocate exactly one environment handle upon starting, and must free it at the end.

    The following code illustrates how to allocate an environment handle:

    SQLHENV env;
    SQLRETURN rc;
    rc = SQLAllocHandle( SQL_HANDLE_ENV, SQL
       _NULL_HANDLE, &env );

  • Connection   A connection is specified by an ODBC driver and a data source. An application can have several connections associated with its environment. Allocating a connection handle does not establish a connection; a connection handle must be allocated first and then used when the connection is established.

    The following code illustrates how to allocate a connection handle:

    SQLHDBC  dbc;
    SQLRETURN rc;
    rc = SQLAllocHandle( SQL_HANDLE_DBC, env, &dbc );

  • Statement   A statement handle provides access to a SQL statement and any information associated with it, such as result sets and parameters. Each connection can have several statements. Statements are used both for cursor operations (fetching data) and for single statement execution (for example, INSERT, UPDATE, and DELETE).

    The following code illustrates how to allocate a statement handle:

    SQLHSTMT stmt;
    SQLRETURN rc;
    rc = SQLAllocHandle( SQL_HANDLE_STMT, dbc, &stmt );


ODBC handles
ODBC example