ConnectionString中的可变密码

时间:2014-08-27 10:21:01

标签: c# connection-string

我创建了连接字符串:

connection.ConnectionString = @"Provider=abcdef; Server=123....; Database=abc; User Id=user; Password=xyz"; 

效果很好。我想用变量交换密码:

connection.ConnectionString = @"Provider=abcdef; Server=123....; Database=abc; User Id=user; Password="+textBox1.Text;

我的程序显示错误:

  

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

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

4 个答案:

答案 0 :(得分:6)

使用SqlConnectionStringBuilder课程。

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

答案 1 :(得分:2)

使用SqlConnectionStringBuilder

var csb = new SqlConnectionStringBuilder(<<your connection string>>);
csb.Password = "xyz";
string connectionString = csb.ConnectionString;

答案 2 :(得分:1)

connection.ConnectionString = string.Format("Provider=abcdef; Server=123....; Database=abc; User Id=user; Password={0}", textBox1.Text);

答案 3 :(得分:0)

你可以试试这个..

string strTextBox;

private void textBox1_TextChanged(object sender, EventArgs e)
{
    strTextBox= textBox1.Text;
}

您使用strTextBox而不是textBox1.Text作为密码。