Skip to main content

How to Put Special Characters in a MySQL Password in a .NET C# Connection String

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A connection string is a list of name=value pairs, split by ;. So a ; in your password ends the value early, and the rest looks like a strange option. Here's every case I tested, and the way to avoid thinking about it.

The Rules​

PasswordWrite it asResult
a=bPassword=a=bConnects: no quotes needed
a bPassword=a bConnects: a space in the middle is fine
abc;defPassword='abc;def' or Password="abc;def"Connects
abc;defPassword=abc;defOption not supported
A space at each endPassword=' ab 'Connects. With no quotes, the spaces are dropped, and you get Access denied
a'bPassword="a'b" or Password='a''b'Connects: double a ' inside single quotes
a"bPassword='a"b' or Password="a""b"Connects: double a " inside double quotes

So the simple habit is single quotes around the password, with any ' inside it doubled.

See the Failure​

With the password abc;def and no quotes:

Output
Unhandled exception. System.ArgumentException: Option not supported (Parameter 'def;sslmode')

Notice what's in that message: def is the end of the password. In an app, that exception lands in your log. If that's happened, treat the password as exposed.

Fix It with Quotes​

Connection string
Server=127.0.0.1;Port=3306;Database=inventory;User ID=quote_demo;Password='abc;def';SslMode=Required
Output
Connected as quote_demo@% to MySQL 9.7.4
TLS: TLSv1.2

The test program printing Connected as quote_demo@% with TLS: TLSv1.2, with the password abc;def in single quotes.

And a password with both an apostrophe and a semicolon, it's;ok, doubled:

Connection string
…;Password='it''s;ok';SslMode=Required
Output
Connected as quote_demo@% to MySQL 9.7.4
TLS: TLSv1.2

These quotes are part of the connection string, and are separate from whatever your shell does. To set it from a terminal, put the whole string in double quotes, and those single quotes inside it.

Or Let the Driver Do It​

If you build the string in code, MySqlConnectionStringBuilder handles every case. This one has ', " and ; together:

connection-string.cs
// The builder quotes every value for you, whatever characters it holds.
var builder = new MySqlConnectionStringBuilder
{
Server = "127.0.0.1",
Port = 3306,
Database = "inventory",
UserID = "inventory_app",
Password = "a'b\"c;d",
SslMode = MySqlSslMode.Required,
};

Console.WriteLine(builder.ConnectionString);
Output
server=127.0.0.1;port=3306;database=inventory;user id=inventory_app;password="a'b""c;d";sslmode=Required

Or Avoid the Problem​

A hex password has no special characters at all, and it's just as strong:

Terminal
openssl rand -hex 24

How to Generate a Strong Random Password in the Terminal explains it. MySQL's own IDENTIFIED BY RANDOM PASSWORD is the opposite: it can produce all of these characters.

Common Mistakes​

MistakeWhat happensDo this instead
Escaping with a backslashThe ; still ends the valueQuote the value
Quoting only in the shellThe shell removes those quotesQuote inside the string
Building the string with $"…Password={password}…"It breaks on the first odd passwordUse the builder

Troubleshooting​

SymptomLikely causeFix
Option not supported (Parameter …)An unquoted ;The fix
Incorrect value in Connection String near 'Password'An opening quote with no closing quoteCheck the quotes match
Access deniedA character changed on its way through a shell, or spaces at the endsThe fix

Conclusion​

Put the password in single quotes, and double any ' inside. Or use MySqlConnectionStringBuilder, or generate hex passwords and skip the problem.

Additional Resources​

Stay Updated

Subscribe to our newsletter for the latest tutorials, tech insights, and developer news.

By subscribing, you agree to our privacy policy. Unsubscribe at any time.