3

I have a Visual Studio 2008 C# .NET 3.5 application where I am trying to connect to a locally hosted MySQL 5.1.53 database using System.Data.Odbc.

using (System.Data.Odbc.OdbcConnection c = new System.Data.Odbc.OdbcConnection("Driver ={MySQL ODBC 5.1 Driver}; Server =localhost; Database =mydatabase; User =root; Option =3; "))
{
    c.Open();
}

Unfortunately, I get the exception: System.Data.Odbc.OdbcException: ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified

I can successfully connect to this database using the VS2008 Server Explorer using the settings:

Data source: .NET Framework Data Provider for ODBC
Use user or system data source name: mydatabase
User name: root

This produces the connection string: Dsn=mydatabase;uid=root Using this connection string in my c# code also yields the same exception.

What do I need to do to connect to this data source successfully in c#?

6
  • 7
    Why not use MySqlConnection instead of Odbc? Commented Sep 16, 2011 at 19:54
  • 2
    Did you try connectionstrings.com for guideance? Commented Sep 16, 2011 at 19:57
  • 2
    Actually it's the IDBConnection interface - not sql connection. Commented Sep 16, 2011 at 20:21
  • @Jon Raynor - Yes, I got the original connection string from there. Commented Sep 16, 2011 at 20:56
  • 1
    I never did get odbc working; I ended up using the MySqlConnection. Commented Oct 11, 2011 at 13:51

1 Answer 1

1

If the connection string is OK, The error sounds like the MySql ODBC driver is not installed on the machine. On windows, there is an ODBC applet which can be used to see if the driver is OK. I believe this is under control panel, administrative tools, Data Sources (ODBC).

I would first check to see if the ODBC driver for MySql is installed using the Windows control panel applet.

A reinstall/update or fresh install of the ODBC driver may be necessary. Here's a link:

http://dev.mysql.com/doc/refman/5.0/en/connector-odbc.html

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, the MySQL ODBC 5.1 Driver is installed. Visual Studio is able to use it to connect to the database, so I assume it is working correctly. The re-install did not make any difference.
Can you connect or create a connection to your data source from the ODBC applet?
Yes. When I click the "Test" button, I get a "Connection successful" message box.

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.