SQLDriverConnectSQLDriverConnect

適用於: 是SQL Server 是Azure SQL Database 是Azure Synapse Analytics (SQL DW) 是平行處理資料倉儲 APPLIES TO: yesSQL Server yesAzure SQL Database yesAzure Synapse Analytics (SQL DW) yesParallel Data Warehouse

SQL ServerSQL Server Native Client ODBC 驅動程式會定義可取代或增強連接字串關鍵字的連接屬性。The SQL ServerSQL Server Native Client ODBC driver defines connection attributes that either replace or enhance connection-string keywords. SQL ServerSQL Server Native Client ODBC 驅動程式已經指定數個連接字串關鍵字的預設值。Several connection-string keywords have default values specified by the SQL ServerSQL Server Native Client ODBC driver.

如需 SQL ServerSQL Server Native Client ODBC 驅動程式中可用的關鍵字清單,請參閱搭配SQL Server Native Client 使用連接字串關鍵字For a list of the keywords available in the SQL ServerSQL Server Native Client ODBC driver, see Using Connection String Keywords with SQL Server Native Client.

如需 SQL ServerSQL Server 連接屬性和驅動程式預設行為的詳細資訊,請參閱SQLSetConnectAttrFor more information about SQL ServerSQL Server connection attributes and driver default behaviors, see SQLSetConnectAttr.

如需對 SQL ServerSQL Server Native Client 有效之連接字串關鍵字的討論,請參閱搭配使用連接字串關鍵字與 SQL Server Native ClientFor a discussion of connection string keywords that are valid for SQL ServerSQL Server Native Client, see Using Connection String Keywords with SQL Server Native Client.

SQLDriverConnect_DriverCompletion_參數值 SQL_DRIVER_PROMPT、SQL_DRIVER_COMPLETE 或 SQL_DRIVER_COMPLETE_REQUIRED 時,SQL ServerSQL Server Native Client ODBC 驅動程式會從所顯示的對話方塊中抓取關鍵字值。When the SQLDriverConnectDriverCompletion parameter value is SQL_DRIVER_PROMPT, SQL_DRIVER_COMPLETE, or SQL_DRIVER_COMPLETE_REQUIRED, the SQL ServerSQL Server Native Client ODBC driver retrieves keyword values from the displayed dialog box. 如果在連接字串中傳遞關鍵字值,而且使用者沒有在對話方塊中變更關鍵字的值,SQL ServerSQL Server Native Client ODBC 驅動程式會使用連接字串中的值。If the keyword value is passed in the connection string and the user does not alter the value for the keyword in the dialog box, the SQL ServerSQL Server Native Client ODBC driver uses the value from the connection string. 如果沒有在連接字串中設定值,而且使用者沒有在對話方塊中進行指派,驅動程式會使用預設值。If the value is not set in the connection string and the user makes no assignment in the dialog box, the driver uses the default.

當有任何DriverCompletion值需要(或可能需要)驅動程式的 [連接] 對話方塊時, SQLDriverConnect必須提供有效的WindowHandleSQLDriverConnect must be given a valid WindowHandle when any DriverCompletion value requires (or could require) the display of the driver's connection dialog box. 無效的控制代碼會傳回 SQL_ERROR。An invalid handle returns SQL_ERROR.

指定 DRIVER 或 DSN 關鍵字。Specify either the DRIVER or DSN keywords. ODBC 敘述如果同時指定兩個關鍵字,驅動程式會使用最左邊的關鍵字,並忽略另一個關鍵字。ODBC states that a driver uses the leftmost of these two keywords and ignores the other if both are specified. 如果已指定 DRIVER,或為兩者的最左邊,而且SQLDriverConnect_DriverCompletion_參數值為 SQL_DRIVER_NOPROMPT,則需要 SERVER 關鍵字和適當的值。If DRIVER is specified, or is the leftmost of the two, and the SQLDriverConnectDriverCompletion parameter value is SQL_DRIVER_NOPROMPT, the SERVER keyword and an appropriate value are required.

指定 SQL_DRIVER_NOPROMPT 時,使用者驗證關鍵字與值必須同時存在。When SQL_DRIVER_NOPROMPT is specified, user authentication keywords must be present with values. 驅動程式會確認字串 "Trusted_Connection=yes" 存在,或 UID 和 PWD 關鍵字同時存在。The driver ensures that either the string "Trusted_Connection=yes" or both the UID and PWD keywords are present.

如果DriverCompletion參數值為 SQL_DRIVER_NOPROMPT 或 SQL_DRIVER_COMPLETE_REQUIRED,而語言或資料庫來自連接字串,且其中一個無效,則SQLDriverConnect會傳回 SQL_ERROR。If the DriverCompletion parameter value is SQL_DRIVER_NOPROMPT or SQL_DRIVER_COMPLETE_REQUIRED and the language or database comes from the connection string and either is invalid, SQLDriverConnect returns SQL_ERROR.

如果DriverCompletion參數值 SQL_DRIVER_NOPROMPT 或 SQL_DRIVER_COMPLETE_REQUIRED,而語言或資料庫來自 ODBC 資料來源定義,而且其中一個無效,則SQLDriverConnect會針對指定的使用者識別碼使用預設的語言或資料庫,並傳回 SQL_SUCCESS_WITH_INFO。If the DriverCompletion parameter value is SQL_DRIVER_NOPROMPT or SQL_DRIVER_COMPLETE_REQUIRED and the language or database comes from the ODBC data source definitions and either is invalid, SQLDriverConnect uses the default language or database for the specified user ID and returns SQL_SUCCESS_WITH_INFO.

如果DriverCompletion參數值 SQL_DRIVER_COMPLETE 或 SQL_DRIVER_PROMPT,而且如果語言或資料庫無效, SQLDriverConnect會重新引發對話方塊。If the DriverCompletion parameter value is SQL_DRIVER_COMPLETE or SQL_DRIVER_PROMPT and if the language or database is invalid, SQLDriverConnect redisplays the dialog box.

高可用性/災害復原的 SQLDriverConnect 支援SQLDriverConnect Support for High Availability, Disaster Recovery

如需使用SQLDriverConnect連線到 AlwaysOn 可用性群組Always On availability groups 叢集的詳細資訊,請參閱高可用性和嚴重損壞修復的 SQL Server Native Client 支援For more information about using SQLDriverConnect to connect to a AlwaysOn 可用性群組Always On availability groups cluster, see SQL Server Native Client Support for High Availability, Disaster Recovery.

服務主要名稱 (SPN) 的 SQLDriverConnect 支援SQLDriverConnect Support for Service Principal Names (SPNs)

SQLDDriverConnect 將會使用 [ODBC 登入] 對話方塊 boxwhen 提示已啟用。SQLDDriverConnect will use the ODBC Login dialog boxwhen prompting is enabled. 如此可允許同時針對主體伺服器和它的容錯移轉夥伴來輸入 SPN。This allows SPNs to be entered for both the principal server and its failover partner.

SQLDriverConnect 將會接受新的連接字串關鍵字ServerSPNFailoverPartnerSPN,而且將會辨識新的連接屬性 SQL_COPT_SS_SERVER_SPN 和 SQL_COPT_SS_FAILOVER_PARTNER_SPN。SQLDriverConnect will accept the new connection string keywords ServerSPN and FailoverPartnerSPN, and will recognize the new connection attributes SQL_COPT_SS_SERVER_SPN and SQL_COPT_SS_FAILOVER_PARTNER_SPN.

當指定連接屬性值一次以上時,以程式設計方式設定的值會優先於 DSN 中的值與連接字串中的值。When a connection attribute value is specified more than once, a value that is set programmatically takes precedence over the value in a DSN and a value in a connection string. DSN 中的值優先於連接字串中的值。A value in a DSN takes precedence over a value in a connection string.

開啟連接時,SQL ServerSQL Server Native Client 會將 SQL_COPT_SS_MUTUALLY_AUTHENTICATED 和 SQL_COPT_SS_INTEGRATED_AUTHENTICATION_METHOD 設定為開啟連接所使用的驗證方法。When a connection is opened, SQL ServerSQL Server Native Client sets SQL_COPT_SS_MUTUALLY_AUTHENTICATED and SQL_COPT_SS_INTEGRATED_AUTHENTICATION_METHOD to the authentication method used to open the connection.

如需 Spn 的詳細資訊,請參閱客戶(端)連接( )ODBC 中的服務主體名稱 spnFor more information about SPNs, see Service Principal Names (SPNs) in Client Connections (ODBC).

範例Examples

下列呼叫說明SQLDriverConnect所需的最少資料量:The following call illustrates the least amount of data required for SQLDriverConnect:

SQLDriverConnect(hdbc, hwnd,  
    (SQLTCHAR*) TEXT("DRIVER={SQL Server Native Client 10};"), SQL_NTS, szOutConn,  
    MAX_CONN_OUT, &cbOutConn, SQL_DRIVER_COMPLETE);  

下列連接字串說明 SQL_DRIVER_NOPROMPT DriverCompletion參數值時所需的最小資料:The following connection strings illustrate minimum required data when the DriverCompletion parameter value is SQL_DRIVER_NOPROMPT:

"DSN=Human Resources;Trusted_Connection=yes"  
  
"FILEDSN=HR_FDSN;Trusted_Connection=yes"  
  
"DRIVER={SQL Server Native Client 10};SERVER=(local);Trusted_Connection=yes"  

另請參閱See Also

SQLDriverConnect 函數 SQLDriverConnect Function
ODBC API 的執行詳細資料 ODBC API Implementation Details
SET ANSI_NULLS (Transact-SQL) SET ANSI_NULLS (Transact-SQL)
SET ANSI_PADDING (Transact-SQL) SET ANSI_PADDING (Transact-SQL)
設定 ANSI_WARNINGS (transact-sql)SET ANSI_WARNINGS (Transact-SQL)