MySql.Data通过Reflection

时间:2015-06-19 13:48:21

标签: c# mysql reflection

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

我的尝试:

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:我已经多次,多次阅读过这个来源(和这一行) - 但是看不到错误:(!

typeMySqlCommand.GetMethod("set_Connection", new Type[] {typeMySqlCommand});

更改为

typeMySqlCommand.GetMethod("set_Connection", new Type[] {typeMySqlConnection});

一切正常!

regrads raiserle

1 个答案:

答案 0 :(得分:0)

解决方案

typeMySqlCommand.GetMethod("set_Connection", new Type[] {typeMySqlCommand});

更改为

typeMySqlCommand.GetMethod("set_Connection", new Type[] {typeMySqlConnection});