user908759
user908759

Reputation: 1355

MySQL OdbcConnection Error System.Data.Odbc.OdbcException

I am trying to connect to a MySQL database using an OdbcConnection in C#. I am getting the following error:

An unhandled exception of type 'System.Data.Odbc.OdbcException' occurred in System.Data.dll

Additional information:

ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified

What is causing this error?

Here is my code:

String connectionString = "Driver=MySQL ODBC 5.2 UNICODE Driver;Server=myserver.com;Database=mydb;User=myusername;Password=mypassword;Option=3;";
OdbcConnection myodbcConnection = new OdbcConnection(connectionString);
OdbcCommand cmd = new OdbcCommand();
OdbcDataReader reader;

cmd.CommandText = sql;
cmd.CommandType = CommandType.Text;
cmd.Connection = myodbcConnection;

myodbcConnection.Open();

reader = cmd.ExecuteReader();

myodbcConnection.Close();

I have also tried the following as connection strings:

"Provider=MSDASQL;Driver={MySQL ODBC 5.2 UNICODE Driver};Server=tcr.cjcresources.com;Database=tcr;User=ahardin;Password=go2sql!;Option=3;";

"Driver={MySQL ODBC 5.2 UNICODE Driver};Server=tcr.cjcresources.com;Database=tcr;User=ahardin;Password=go2sql!;Option=3;";

"DSN={MySQL ODBC 5.2 UNICODE Driver};Server=tcr.cjcresources.com;Database=tcr;User=ahardin;Password=go2sql!;Option=3;";

and many more the like.

My setting for Control Panel->Administrative Tools->Data Sources (ODBC)

User DSN has: MySQL ODBC 5.2 UNICODE Drive & MySQL ODBC 5.2 ANSI Drive

System DSN has: MySQL ODBC 5.2 UNICODE Drive & MySQL ODBC 5.2 ANSI Drive

File DSN has: MySQL ODBC 5.2 UNICODE Drive & MySQL ODBC 5.2 ANSI Drive in a ODBC folder

I also have MySQL Workbench that is able to connect to the remote Database

Any help would be greatly appreciated

Upvotes: 1

Views: 8185

Answers (1)

bohica
bohica

Reputation: 5992

"Driver=MySQL ODBC 5.2 UNICODE Driver;Server=myserver.com;Database=mydb;User=myusername;Password=mypassword;Option=3;"

should be

"DRIVER={MySQL ODBC 5.2 UNICODE Driver};Server=myserver.com;Database=mydb;USER=myusername;PASSWORD=mypassword;Option=3;"

Your driver name contains spaces so you delimit with { and }. Personally, for ODBC defined attributes, I'd always use uppercase as well I've come across driver managers that only do uppercase.

Upvotes: 1

Related Questions