SqlCredential クラス

定義

SqlCredential は SQL Server 認証を使用してログイン試行のパスワードを指定するより安全な方法を提供します。

SqlCredential は SQL Server の認証に使用するユーザー ID とパスワードで構成されます。 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 オブジェクトを取得または設定します。 オブジェクトのパスワードを変更するには、 をSqlCredential使用ChangePasswordします。 オブジェクトが接続プールのSqlCredential動作にどのように影響するかについては、「SQL Server接続プール (ADO.NET)」を参照してください。

null 以外SqlCredentialInvalidOperationExceptionオブジェクトが次のいずれかの接続文字列キーワードとの接続で使用されている場合、例外が発生します。

  • Integrated Security = true

  • Password

  • User ID

次の例では、 を使用して CredentialSQL Server データベースに接続します。

// change connection string in the APP.CONFIG file  
<connectionStrings>  
  <add name="MyConnString"  
       connectionString="Initial Catalog=myDB;Server=myServer"  
       providerName="Microsoft.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 オブジェクトのユーザー ID コンポーネントを取得します。

適用対象