SqlCredential 類別

定義

SqlCredential 提供更安全的方法,以指定使用 SQL Server 驗證的登入密碼。

SqlCredential 由將用於 SQL Server 驗證的使用者識別碼和密碼組成。 SqlCredential 物件中的密碼是 SecureString 型別。

SqlCredential 無法被繼承。

Windows 驗證 (Integrated Security = true) 會維持最安全的方式,來登入 SQL Server 資料庫。

public ref class SqlCredential sealed
public sealed class SqlCredential
type SqlCredential = class
Public NotInheritable Class SqlCredential
繼承
SqlCredential

備註

用來 Credential 取得或設定連接的物件 SqlCredential 。 使用 ChangePassword 透過物件變更用戶 SqlCredential 的密碼。 如需物件如何影響SqlCredential連接集區行為的資訊,請參閱 SQL Server 連線共用 (ADO.NET)

InvalidOperationException如果在與下列任一 連接字串 關鍵詞的連線中使用非 Null SqlCredential 物件,將會引發例外狀況:

  • Integrated Security = true

  • Password

  • User ID

  • Context Connection = true

下列範例會使用 Credential連接到 SQL Server 資料庫:

// change connection string in the APP.CONFIG file  
<connectionStrings>  
  <add name="MyConnString"  
       connectionString="Initial Catalog=myDB;Server=myServer"  
       providerName="System.Data.SqlClient" />  
</connectionStrings>  

// then use the following snippet:  
using System.Configuration;  

System.Windows.Controls.TextBox txtUserId = new System.Windows.Controls.TextBox();  
System.Windows.Controls.PasswordBox txtPwd = new System.Windows.Controls.PasswordBox();  

Configuration config = Configuration.WebConfigurationManager.OpenWebConfiguration(Null);  
ConnectionStringSettings connString = config.ConnectionStrings.ConnectionString["MyConnString"];  

using (SqlConnection conn = new SqlConnection(connString.ConnectionString))  
{  
SecureString pwd = txtPwd.SecurePassword;  
pwd.MakeReadOnly();  
SqlCredential cred = new SqlCredential(txtUserId.Text, pwd);  
conn.Credential = cred;  
conn.Open();  

建構函式

SqlCredential(String, SecureString)

建立類型為 SqlCredential 的物件。

屬性

Password

取得 SqlCredential 物件的密碼元件。

UserId

取得 SqlCredential 物件的使用者識別碼元件。

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於

另請參閱