C# 连接 MySql 数据库
我有一个 Visual Studio 2008 C# .NET 3.5 应用程序,我尝试使用 System.Data.Odbc 连接到本地托管的 MySQL 5.1.53 数据库。
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();
}
不幸的是,我得到了异常: System.Data.Odbc.OdbcException: ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not find and no default driver specified
我可以成功连接到此使用以下设置使用 VS2008 服务器资源管理器访问数据库:
Data source: .NET Framework Data Provider for ODBC
Use user or system data source name: mydatabase
User name: root
这会产生连接字符串:Dsn=mydatabase;uid=root
在我的 C# 代码中使用此连接字符串也会产生相同的异常。
我需要做什么才能在 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#?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果连接字符串正常,则错误听起来像是机器上未安装 MySql ODBC 驱动程序。在 Windows 上,有一个 ODBC 小程序可用于查看驱动程序是否正常。我相信这是在控制面板、管理工具、数据源 (ODBC) 下。
我首先检查是否使用 Windows 控制面板小程序安装了 MySql 的 ODBC 驱动程序。
可能需要重新安装/更新或全新安装 ODBC 驱动程序。这是一个链接:
http://dev.mysql.com/doc /refman/5.0/en/connector-odbc.html
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