How to: Allocate Handles and Connect to SQL Server (ODBC)
To allocate handles and connect to SQL Server
Include the ODBC header files Sql.h, Sqlext.h, Sqltypes.h.
Include the SQL Server driver-specific header file, Odbcss.h.
Call SQLAllocHandle with a HandleType of SQL_HANDLE_ENV to initialize ODBC and allocate an environment handle.
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.
Optionally, call SQLSetEnvAttr to set other environment options, or call SQLGetEnvAttr to get environment options.
Call SQLAllocHandle with a HandleType of SQL_HANDLE_DBC to allocate a connection handle.
Optionally, call SQLSetConnectAttr to set connection options, or call SQLGetConnectAttr to get connection options.
Call SQLConnect to use an existing data source to connect to SQL Server.
Or
Call SQLDriverConnectto use a connection string to connect to SQL Server.
A minimum complete SQL Server connection string has one of two forms:
DSN=dsn_name;Trusted_connection=yes; DRIVER={SQL Server Native Client 10.0};SERVER=server;Trusted_connection=yes;
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.
Optionally, call SQLGetInfo to get driver attributes and behavior for the SQL Server data source.
Allocate and use statements.
Call SQLDisconnect to disconnect from SQL Server and make the connection handle available for a new connection.
Call SQLFreeHandle with a HandleType of SQL_HANDLE_DBC to free the connection handle.
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 crypto API. |
Example
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 Native Client 10.0};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);