Connecting to MySql database in C#
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#?
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
精彩评论