SqlConnectionStringBuilder.Password 属性

定义

获取或设置 SQL Server 帐户的密码。

public:
 property System::String ^ Password { System::String ^ get(); void set(System::String ^ value); };
public string Password { get; set; }
member this.Password : string with get, set
Public Property Password As String

属性值

Password 属性的值,或者,如果未提供任何值,则为 String.Empty

例外

密码错误地设置为 null。 请参阅下面的代码示例。

示例

以下示例演示如何设置 Password

using Microsoft.Data.SqlClient;

class Program
{
    public static void Main()
    {
        SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();

        builder["Password"] = null;
        string aa = builder.Password;
        Console.WriteLine(aa.Length);

        builder["Password"] = "??????";
        aa = builder.Password;
        Console.WriteLine(aa.Length);

        try
        {
            builder.Password = null;
        }
        catch (ArgumentNullException e)
        {
            Console.WriteLine("{0}", e);
        }
    }
}

注解

此属性与连接字符串内的“Password”和“pwd”键相对应。

不建议设置此属性。 为了保持高级别的安全性,我们强烈建议改用 Integrated SecurityTrusted_Connection 关键字 (keyword) 。 SqlCredential是为使用 SQL Server 身份验证的连接指定凭据的更安全方法。

如果 Password 尚未设置并且检索值,则返回值为 Empty。 若要重置连接字符串的密码,请将 null 传递给 Item 属性。

密码必须为 128 个字符或更少。

适用于