Export (0) Print
Expand All

How to allocate handles and connect to SQL Server (ODBC)

SQL Server 2000

  New Information - SQL Server 2000 SP3.

To allocate handles and connect to SQL Server

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

  2. Include the Microsoft® SQL Server™ 2000 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 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 SQLGetConnectAttr to get connection options.

  8. Call SQLConnect to use an existing data source to connect to SQL Server.

    Or

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

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

    DSN=dsn_name;UID=login_id;PWD=password;

    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 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.

Security 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 cryptoAPI.

Examples
A. Allocate handles, then connect by using SQLConnect

The example shows allocating an environment handle and a connection handle, then connecting by using SQLConnect. It has been simplified by removing much of the error checking.

The complete sample code is in this file: AllocateEnvironmentHandleAndConnectionHandle.cpp. You can download a file containing this sample from the SQL Server Downloads page at this Microsoft Web site.

This sample was developed for ODBC version 3.0 or later. It was developed with Microsoft Visual C++® version 6.0, and may expose properties of the Microsoft Foundation Classes.

B. Connect to SQL Server without an existing ODBC data source

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

SQLHENV      henv = SQL_NULL_HENV;
SQLHDBC      hdbc1 = SQL_NULL_HDBC;
SQLHSTMT      hstmt1 = SQL_NULL_HSTMT;

SQLCHAR      ConnStrIn[MAXBUFLEN] =
         "DRIVER={SQL Server};SERVER=MyServer";

SQLCHAR      ConnStrOut[MAXBUFLEN];
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
// SQL_DRIVER_NOPROMPT.
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
                  SQL_DRIVER_PROMPT);

See Also

SQLFreeHandle

SQLGetInfo

SQLBrowseConnect

SQLSetConnectAttr

SQLDriverConnect

SQLSetEnvAttr

SQLGetConnectAttr

Show:
© 2015 Microsoft