Wednesday, February 27, 2013

Unknown database 'your_database' error in MySQL in app.config

There's a weird scenario when retrieving table names from a specific database, upon querying, using connection.Open() in C#, an error such as "Unknown database 'database_name'" shows in the exception object.

I was simply checking the database name using MySQL Browser and found out that the database name starts with a space. So, after thinking about it, I decided to modify the app.config and changed the value of the connection string by enclosing the name with single quotes to include the space character of the database name.

Not working app.config setting:
 <add key="try" value="server=;database=database_v1;Uid=james;pwd=pass123;"/>  
Working app.config setting with database name enclosed in single quotes:
 <add key="try" value="server=;database=' database_v1';Uid=james;pwd=pass123;"/>  

Note: This fix is applied towards database names that starts/ends with space.
Cheers! That was a quick fix! :D



Post a Comment