分配句柄并连接到 SQL Server (ODBC)Allocate Handles and Connect to SQL Server (ODBC)

适用于:Applies to: 是SQL ServerSQL Server(所有支持的版本)yesSQL ServerSQL Server (all supported versions) 是Azure SQL 数据库Azure SQL DatabaseYesAzure SQL 数据库Azure SQL Database 是Azure SQL 托管实例Azure SQL Managed InstanceYesAzure SQL 托管实例Azure SQL Managed Instance 是Azure Synapse AnalyticsAzure Synapse AnalyticsyesAzure Synapse AnalyticsAzure Synapse Analytics 是并行数据仓库Parallel Data Warehouseyes并行数据仓库Parallel Data Warehouse适用于:Applies to: 是SQL ServerSQL Server(所有支持的版本)yesSQL ServerSQL Server (all supported versions) 是Azure SQL 数据库Azure SQL DatabaseYesAzure SQL 数据库Azure SQL Database 是Azure SQL 托管实例Azure SQL Managed InstanceYesAzure SQL 托管实例Azure SQL Managed Instance 是Azure Synapse AnalyticsAzure Synapse AnalyticsyesAzure Synapse AnalyticsAzure Synapse Analytics 是并行数据仓库Parallel Data Warehouseyes并行数据仓库Parallel Data Warehouse

分配句柄并连接到 SQL ServerTo allocate handles and connect to SQL Server

  1. 包含 ODBC 头文件 Sql.h、Sqlext.h、Sqltypes.h。Include the ODBC header files Sql.h, Sqlext.h, Sqltypes.h.

  2. 包含特定于 SQL ServerSQL Server 驱动程序的头文件 Odbcss.h。Include the SQL ServerSQL Server driver-specific header file, Odbcss.h.

  3. 使用 SQL_HANDLE_ENV 的 HandleType 调用 SQLALLOCHANDLE以初始化 ODBC 并分配环境句柄。Call SQLAllocHandle with a HandleType of SQL_HANDLE_ENV to initialize ODBC and allocate an environment handle.

  4. 调用 SQLSetEnvAttr 并将 属性 设置为 SQL_ATTR_ODBC_VERSION,并将 将 valueptr 设置为 SQL_OV_ODBC3,以指示应用程序将使用 ODBC 1.x 格式函数调用。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. 也可以调用 SQLSetEnvAttr 来设置其他环境选项,或调用 SQLGetEnvAttr 来获取环境选项。Optionally, call SQLSetEnvAttr to set other environment options, or call SQLGetEnvAttr to get environment options.

  6. 使用 SQL_HANDLE_DBC 的 HandleType 调用 SQLAllocHandle ,以分配连接句柄。Call SQLAllocHandle with a HandleType of SQL_HANDLE_DBC to allocate a connection handle.

  7. 也可以调用 SQLSetConnectAttr 来设置连接选项,或调用 SQLGetConnectAttr 来获取连接选项。Optionally, call SQLSetConnectAttr to set connection options, or call SQLGetConnectAttr to get connection options.

  8. 调用 SQLConnect,以使用现有数据源连接到 SQL ServerSQL ServerCall SQLConnect to use an existing data source to connect to SQL ServerSQL Server.

    Or

    调用 SQLDriverConnect 以使用连接字符串连接 SQL ServerSQL ServerCall SQLDriverConnect to use a connection string to connect to SQL ServerSQL Server.

    最小的完整 SQL ServerSQL Server 连接字符串采用以下两种格式之一:A minimum complete SQL ServerSQL 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;  
    

    如果连接字符串不完整, SQLDriverConnect 可能会提示输入所需的信息。If the connection string is not complete, SQLDriverConnect can prompt for the required information. 这是由为 DriverCompletion 参数指定的值控制的。This is controlled by the value specified for the DriverCompletion parameter.

    - 或 -- or -

    多次调用 SQLBrowseConnect 以生成连接字符串并连接到 SQL ServerSQL ServerCall SQLBrowseConnect multiple times in an iterative fashion to build the connection string and connect to SQL ServerSQL Server.

  9. 也可以调用 SQLGetInfo 来获取数据源的驱动程序属性和行为 SQL ServerSQL ServerOptionally, call SQLGetInfo to get driver attributes and behavior for the SQL ServerSQL Server data source.

  10. 分配并使用语句。Allocate and use statements.

  11. 调用 SQLDisconnect 断开连接 SQL ServerSQL Server ,并使连接句柄可用于新连接。Call SQLDisconnect to disconnect from SQL ServerSQL Server and make the connection handle available for a new connection.

  12. 使用 SQL_HANDLE_DBC 的 HandleType 调用 SQLFreeHandle ,以释放连接句柄。Call SQLFreeHandle with a HandleType of SQL_HANDLE_DBC to free the connection handle.

  13. 使用 SQL_HANDLE_ENV 的 HandleType 调用 SQLFreeHandle ,以释放环境句柄。Call SQLFreeHandle with a HandleType of SQL_HANDLE_ENV to free the environment handle.

重要

请尽可能使用 Windows 身份验证。When possible, use Windows Authentication. 如果 Windows 身份验证不可用,请在运行时提示用户输入其凭据。If Windows Authentication is not available, prompt users to enter their credentials at run time. 不要将凭据存储在一个文件中。Avoid storing credentials in a file. 如果必须保存凭据,应当用 Win32 crypto API(Win32 加密 API)加密它们。If you must persist credentials, you should encrypt them with the Win32 crypto API.

示例Example

此示例演示如何调用 SQLDriverConnect 以连接到实例, SQL ServerSQL Server 而无需使用现有的 ODBC 数据源。This example shows a call to SQLDriverConnect to connect to an instance of SQL ServerSQL Server without requiring an existing ODBC data source. 通过将不完整的连接字符串传递给 SQLDriverConnect,它会使 ODBC 驱动程序提示用户输入缺少的信息。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);