C# .Net MysqlConnector ConnectionString Password Problem / Issue

Thomas :

need help with some stupid password problems. Following problem:

Cannot connect to mysql server with some passwords ^^

If i use a password with the same "connection string" details, for example:

The following Passwords are exactly in the MySql Server (quotes, double-quotes, semicolon);

"pwd=123456"; // works

"pwd=123456;"; // works

";pwd=123;456;' // System.ArgumentException

";pwd=123;456;'" // System.ArgumentException

//Connection String Variant 1:

string connString = string.Format("server={0};port={1};uid={2};pwd={3};database={4};sslmode=preferred",
                tbDbServername.Text.ToLower(),
                (uint)Convert.ToInt32(tbDbServerPort.Text),
                tbDbServerUsername.Text.ToLower(),
                tbDbServerPassword.Password,
                tbDbServerDatabase.Text.ToLower()
                );

//Connection String Variant 2:

string connString = string.Format("server={0};port={1};uid={2};pwd='{3}';database={4};sslmode=preferred",
                tbDbServername.Text.ToLower(),
                (uint)Convert.ToInt32(tbDbServerPort.Text),
                tbDbServerUsername.Text.ToLower(),
                tbDbServerPassword.Password,
                tbDbServerDatabase.Text.ToLower()
                );

I know it's stupid. Why should someone use a password with connection string details....but would be nice to solve this.

Greetings from Germany :)

madreflection :

Use a MySqlConnectionStringBuilder to construct your connection string. It will do any escaping/quoting that's needed.

In your case, you would want to do this:

MySqlConnectionStringBuilder builder = new MySqlConnectionStringBuilder()
{
    Server = tbDbServername.Text.ToLower(),
    Port = (uint)System.Convert.ToInt32(tbDbServerPort.Text),
    UserID = tbDbServerUsername.Text.ToLower(),
    Password = tbDbServerPassword.Password,
    Database = tbDbServerDatabase.Text.ToLower(),
    SslMode = MySqlSslMode.Preferred
};

string connString = builder.ConnectionString;

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=5569&siteId=1