How to: Allocate Handles and Connect to SQL Server (ODBC)

New: 14 April 2006

  1. Include the ODBC header files Sql.h, Sqlext.h, Sqltypes.h.

  2. Include the SQL Server 2005 driver-specific header file, Odbcss.h.

  3. Call SQLAllocHandle with a HandleType of SQL_HANDLE_ENV to initialize ODBC and allocate an environment handle.

  4. Call SQLSetEnvAttr with Attribute set to SQL_ATTR_ODBC_VERSION and ValuePtr set to SQL_OV_ODBC3 to indicate the application will use ODBC 3.x-format function calls.

  5. Optionally, call SQLSetEnvAttr to set other environment options, or call SQLGetEnvAttr to get environment options.

  6. Call SQLAllocHandle with a HandleType of SQL_HANDLE_DBC to allocate a connection handle.

  7. Optionally, call SQLSetConnectAttr to set connection options, or call SQLGetConnectAttr to get connection options.

  8. Call SQLConnect (see Connecting with SQLConnect) to use an existing data source to connect to SQL Server.


    Call SQLDriverConnect to use a connection string to connect to SQL Server.

    A minimum complete SQL Server connection string has one of two forms:

    DRIVER={SQL Server};SERVER=server;UID=login_id;PWD=password;

    If the connection string is not complete, SQLDriverConnect can prompt for the required information. This is controlled by the value specified for the DriverCompletion parameter.

    - or -

    Call SQLBrowseConnect multiple times in an iterative fashion to build the connection string and connect to SQL Server.

  9. Optionally, call SQLGetInfo to get driver attributes and behavior for the SQL Server data source.

  10. Allocate and use statements.

  11. Call SQLDisconnect Function to disconnect from SQL Server and make the connection handle available for a new connection.

  12. Call SQLFreeHandle with a HandleType of SQL_HANDLE_DBC to free the connection handle.

  13. Call SQLFreeHandle with a HandleType of SQL_HANDLE_ENV to free the environment handle.,SQL.90).gifSecurity Note:
When possible, use Windows Authentication. If Windows Authentication is not available, prompt users to enter their credentials at run time. Avoid storing credentials in a file. If you must persist credentials, you should encrypt them with the Win32 crypto API.

This example shows a call to SQLDriverConnect to connect to an instance of SQL Server without requiring an existing ODBC data source. By passing an incomplete connection string to SQLDriverConnect, it causes the ODBC driver to prompt the user to enter the missing information.

#define MAXBUFLEN   255


         "DRIVER={SQL Server};SERVER=MyServer";

SQLSMALLINT   cbConnStrOut = 0;

// Make connection without data source. Ask that driver 
// prompt if insufficient information. Driver returns
// SQL_ERROR and application prompts user
// for missing information. Window handle not needed for
retcode = SQLDriverConnect(hdbc1,      // Connection handle
                  NULL,         // Window handle
                  ConnStrIn,      // Input connect string
                  SQL_NTS,         // Null-terminated string
                  ConnStrOut,      // Address of output buffer
                  MAXBUFLEN,      // Size of output buffer
                  &cbConnStrOut,   // Address of output length

Community Additions