将 Odbc 引用添加到我的应用程序

Posted

技术标签:

【中文标题】将 Odbc 引用添加到我的应用程序【英文标题】:Adding Odbc reference to my application 【发布时间】:2014-07-13 18:33:01 【问题描述】:

我正在使用 Visual Studio 2013 使用 ODBC 连接到 mysql 数据库。我无法在我的项目中找到 System.Data.Odbc 的参考。我不断收到此异常

 System.Data.Odbc.OdbcException (0x80131937): ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
   at System.Data.Odbc.OdbcConnection.HandleError(OdbcHandle hrHandle, RetCode retcode)
   at System.Data.Odbc.OdbcConnectionHandle..ctor(OdbcConnection connection, OdbcConnectionString constr, OdbcEnvironmentHandle environmentHandle)
   at System.Data.Odbc.OdbcConnectionOpen..ctor(OdbcConnection outerConnection, OdbcConnectionString connectionOptions)
   at System.Data.Odbc.OdbcConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningObject)
   at System.Data.ProviderBase.DbConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningCo
nnection, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, Db
ConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions use
rOptions)
   at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionInternal.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
   at System.Data.Odbc.OdbcConnection.Open()
   at DbConnection.Program.Main(String[] args) in C:\Users\ankur_agrawal\AppData\Local\Temporary Projects\DbConnection\Program.cs:line 17System.InvalidOperationException: ExecuteReader requires an ope
n and available Connection. The connection's current state is closed.
   at System.Data.Odbc.OdbcConnection.SetStateExecuting(String method, OdbcTransaction transaction)
   at System.Data.Odbc.OdbcCommand.ValidateConnectionAndTransaction(String method)
   at System.Data.Odbc.OdbcCommand.ExecuteReaderObject(CommandBehavior behavior, String method, Boolean needReader, Object[] methodArguments, SQL_API odbcApiMethod)
   at System.Data.Odbc.OdbcCommand.ExecuteReaderObject(CommandBehavior behavior, String method, Boolean needReader)
   at System.Data.Odbc.OdbcCommand.ExecuteReader(CommandBehavior behavior)

我写的代码是

namespace DB

    class Program
    
        static void Main(string[] args)
        
            try
            
                Program p = new Program();
                OdbcConnection con = new OdbcConnection("DRIVER=MySQL ODBC 3.51 Driver;SERVER=localhost;USER=root;PASSWORD=root;DATABASE=employee");
                OdbcCommand cmd = new OdbcCommand("select * from emplpoyee", con);
                con.Open();
                OdbcDataReader reader = cmd.ExecuteReader();
                reader.Close();
            

            catch (Exception e)
            
                Console.Write(e);
            

            Console.ReadKey();
        
    

【问题讨论】:

您使用的是哪个 MySQL 版本?您必须使用Driver 部分吗? This link 建议使用 Driver=MySQL ODBC 5.2 UNICODE DriverDriver=MySQL ODBC 5.2 ANSI Driver 做到了,还是出现异常 你安装了这个dev.mysql.com/downloads/connector/odbc吗? 【参考方案1】:

如果您使用的是 WINDOWS-8 ,您将无法连接到 MYSQL DB .. 要解决此问题,您应该运行与 Win7 兼容的 Visual Studio。

【讨论】:

以上是关于将 Odbc 引用添加到我的应用程序的主要内容,如果未能解决你的问题,请参考以下文章

将托管在私有 *** 中的服务引用添加到我的项目

我是不是需要在需要时手动将每个 *.m 文件添加到我的单元测试目标?

JSP - Microsoft ODBC - Microsoft Access 驱动程序找不到文件“(未知)”

将 Winsock2 添加到我的 MinGW-w64 C/C++ 会导致:未定义对“InitializeConditionVariable”的引用

ssis 找不到任何具有 odbc 连接的表

如何将 System.Web 引用添加到 Windows 窗体应用程序