Establish ODBC connections

Use these handle types to establish an IBM® i Access ODBC connection.

SQLAllocHandle with SQL_HANDLE_ENV as the handle type
  • Allocates memory for an environment handle.
    • Identifies storage for global information:
      • Valid connection handles
      • Variable type HENV
  • Must be called by application prior to calling any other ODBC function.
  • Variable type HENV is defined by ODBC in the SQL.H header file provided by the C programming language compiler or by the ODBC Software Development Kit (SDK).

    The header file contains a type definition for a far pointer:

      typedef void far * HENV
  • In C programming language this statement is coded:
    SQLRETURN rc;
    HENV henv;
    
    rc = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
  • In Visual Basic, this statement is coded:
    Dim henv As long
    SQLAllocEnv(henv)
SQLAllocHandle with SQL_HANDLE_DBC as the handle type
  • Allocates memory for an connection handle within the environment.
    • Identifies storage for information about a particular connection.
      • Variable type HDBC
      • Application can have multiple connection handles.
  • Application must request a connection handle prior to connecting to the data source.
  • In C, this statement is coded:
    HDBC hdbc;
    
    rc = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
  • In Visual Basic, this statement is coded:
    Dim hdbc As long
    SQLAllocConnect(henv,hdbc)
SQLSetEnvAttr
  • Allows an application to set attributes of an environment.
  • To be considered an ODBC 3.x application, you must set the SQL_ATTR_ODBC_VERSION to SQL_OV_ODBC3 prior to allocating a connection handle.
  • In C, this statement is coded:
    rc = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER) SQL_OV_ODBC3, SQL_IS_UINTEGER);
SQLConnect
  • Loads driver and establishes a connection.
  • Connection handle references information about the connection.
  • Data source is coded into application.

    In C, this statement is coded:

    SQLCHAR source[ ] = "myDSN";
    SQLCHAR uid[ ] = "myUID";
    SQLCHAR pwd[ ] = "myPWD";
    
    rc = SQLConnect(hdbc, source, SQL_NTS, uid, SQL_NTS, pwd, SQL_NTS);
    Note: SQL_NTS indicates that the parameter string is a null-terminated string.
SQLDriverConnect
  • Alternative to SQLConnect
  • Allows application to override data source settings.
  • Displays dialog boxes (optional).