3

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 :)

2 Answers 2

6

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;
Sign up to request clarification or add additional context in comments.

2 Comments

Too easy. Thank you very much. Works :)
Glad to be of assistance.
1

Certain characters are not allowed in connection string like [] {}() , ; ? * ! @ =. without using escape-character. The use of ; as part of password has caused problem as its expected to be used as delimiter

The special notes on special characters is mentioned at relatively odd place in Microsoft documentation:

Notes on special character

If used in an OLE DB or ODBC connection string, a login or password must not contain the following characters: [] {}() , ; ? * ! @ =. These characters are used to either initialize a connection or separate connection values.

ConnectionString

The basic format of a connection string includes a series of keyword/value pairs separated by semicolons. The equal sign (=) connects each keyword and its value. To include values that contain a semicolon, single-quote character, or double-quote character, the value must be enclosed in double quotation marks. If the value contains both a semicolon and a double-quote character, the value can be enclosed in single quotation marks. The single quotation mark is also useful if the value starts with a double-quote character. Conversely, the double quotation mark can be used if the value starts with a single quotation mark. If the value contains both single-quote and double-quote characters, the quotation mark character used to enclose the value must be doubled every time it occurs within the value.

6 Comments

@Thomas A worth reading for connection string formatr MySQL at dev.mysql.com/doc/refman/8.0/en/…. Please note that when we connect from C# assembly then it connects via MySQL/ODBC connector.
All those characters can be used in MySQL user passwords, and can be used in a MySQL connection string if escaped properly (use MySqlConnectionStringBuilder to do this).
@bradleyGraingrr I agree it can be used with proper escape character and it's mention in link with my answer too. I don't understand why confusion came?
Your answer states in its first sentence: "Certain characters are not allowed in connection string like [] {}() , ; ? * ! @ =.". This is not true for MySQL, which is what the question is about.
@BradleyGrainger I meant without escape character or other formats. I over looked as it was mentioned in link. Moreover the second link for specifically MySQL it's states that for MySQL connection string can URI-like connection strings or key-value pairs. But point is when connection string is used via MySQL/ODBC connectors those characters are not allowed without escape-characters.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.