首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MySql.Data反射

MySql.Data反射
EN

Stack Overflow用户
提问于 2015-06-19 13:48:21
回答 1查看 667关注 0票数 2

我使用EPLAN脚本,我想连接一个数据库。脚本引擎允许在运行时加载程序集。

我的尝试:

代码语言:javascript
复制
Assembly assMySqlData = Assembly.LoadFrom(@"c:\Program Files (x86)\MySQL\MySQL Connector Net 6.9.6\Assemblies\v4.0\MySql.Data.dll");

        Type typeMySqlConnection = assMySqlData.GetType("MySql.Data.MySqlClient.MySqlConnection");
        Type typeMySqlCommand = assMySqlData.GetType("MySql.Data.MySqlClient.MySqlCommand");
        Type typeMySqlDataReader = assMySqlData.GetType("MySql.Data.MySqlClient.MySqlDataReader");


        object oMySqlConnection = Activator.CreateInstance(typeMySqlConnection);

        if (oMySqlConnection != null)
        {
            MethodInfo methodMysqlConnection_State = typeMySqlConnection.GetMethod("get_State");
            MethodInfo methodMysqlConnection_Open = typeMySqlConnection.GetMethod("Open");
            MethodInfo methodMysqlConnection_ConnectionString = typeMySqlConnection.GetMethod("set_ConnectionString");
            MethodInfo methodMysqlConnection_Close = typeMySqlConnection.GetMethod("Close");
            MethodInfo methodMysqlConnection_Dispose = typeMySqlConnection.GetMethod("Dispose");

            MessageBox.Show(typeMySqlConnection.ToString());
            MessageBox.Show(oMySqlConnection.ToString());

            MethodInfo methodMysqlCommand_ExecuteReader = typeMySqlCommand.GetMethod("ExecuteReader",new Type[]{});
            MethodInfo methodMysqlCommand_Connection = typeMySqlCommand.GetMethod("set_Connection", new Type[] {typeMySqlCommand});
            MethodInfo methodMysqlCommand_CommandText = typeMySqlCommand.GetMethod("set_CommandText");
            MethodInfo methodMysqlCommand_Dispose = typeMySqlCommand.GetMethod("Dispose");

            MethodInfo methodMysqlDataReader_Read = typeMySqlDataReader.GetMethod("Read");
            MethodInfo methodMysqlDataReader_HasRows = typeMySqlDataReader.GetMethod("get_HasRows");
            MethodInfo methodMysqlDataReader_FieldCount = typeMySqlDataReader.GetMethod("get_FieldCount");
            MethodInfo methodMysqlDataReader_GetValue = typeMySqlDataReader.GetMethod("GetValue");
            MethodInfo methodMysqlDataReader_Close = typeMySqlDataReader.GetMethod("Close");
            MethodInfo methodMysqlDataReader_Dispose = typeMySqlDataReader.GetMethod("Dispose",new Type[] {});


            object[] arg = new object[] { (string)"Server=server;Port=3307;Database=db;Uid=me;Pwd=123456;" };
            methodMysqlConnection_ConnectionString.Invoke(oMySqlConnection, arg);
            methodMysqlConnection_Open.Invoke(oMySqlConnection, null);
            object mysqlState = methodMysqlConnection_State.Invoke(oMySqlConnection, null);
            MessageBox.Show(mysqlState.ToString());

            object oMysqlCommand = Activator.CreateInstance(typeMySqlCommand);
            if (oMysqlCommand != null)
            {
                object[] paramCommandConnection = new object[] { oMySqlConnection }; //oMysqlConnection is not null
                object[] paramCommandText = new object[] { "SELECT * FROM `config`" };
                methodMysqlCommand_CommandText.Invoke(oMysqlCommand, paramCommandText);  //works fine
                methodMysqlCommand_Connection.Invoke(oMysqlCommand, paramCommandConnection); // error no object
                MessageBox.Show("5");
                object oMysqlDataReader = methodMysqlCommand_ExecuteReader.Invoke(oMysqlCommand, null);
                MessageBox.Show("6");
                object retRows = methodMysqlDataReader_HasRows.Invoke(oMysqlDataReader, null);
                MessageBox.Show("7");
                MessageBox.Show(retRows.ToString());
                MessageBox.Show("8");
            }

        }

我在methodMysqlCommand_Connection.Invoke (oMysqlCommand, paramCommandConnection);no instance of object上有一个错误。我的错误在哪里?oMySqlConnection不是null,连接状态是open

解决方案

OMG:我读过这个来源(和这行)很多很多..。很多次-但看不到错误:(!)

代码语言:javascript
复制
typeMySqlCommand.GetMethod("set_Connection", new Type[] {typeMySqlCommand});

更改为

代码语言:javascript
复制
typeMySqlCommand.GetMethod("set_Connection", new Type[] {typeMySqlConnection});

一切都很好!

雷格葡萄干

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-27 11:17:14

解决方案

代码语言:javascript
复制
typeMySqlCommand.GetMethod("set_Connection", new Type[] {typeMySqlCommand});

更改为

代码语言:javascript
复制
typeMySqlCommand.GetMethod("set_Connection", new Type[] {typeMySqlConnection});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30939824

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档