ConnectionString中的可变密码

1 c# connection-string

我创建了连接字符串:

connection.ConnectionString = @"Provider=abcdef; Server=123....; Database=abc; User Id=user; Password=xyz"; 
Run Code Online (Sandbox Code Playgroud)

它工作得很好.我想用变量交换密码:

connection.ConnectionString = @"Provider=abcdef; Server=123....; Database=abc; User Id=user; Password="+textBox1.Text;
Run Code Online (Sandbox Code Playgroud)

我的程序显示错误:

用户登录失败:user ...(textBox.Text ="xyz")

你知道如何正确地用变量交换密码吗?

Sri*_*vel 6

使用SqlConnectionStringBuilder类.

static void Main()
{
    SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connectionString);
    builder.Password = "YourNewPassword";
    string newConnectionString = builder.ConnectionString;
}
Run Code Online (Sandbox Code Playgroud)