C# MySql。通过反射获取数据
我使用EPLAN脚本,我想连接一个数据库。 脚本引擎允许在运行时加载程序集 我的尝试:C# MySql。通过反射获取数据,c#,mysql,reflection,C#,Mysql,Reflection,我使用EPLAN脚本,我想连接一个数据库。 脚本引擎允许在运行时加载程序集 我的尝试: 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.MySq
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(mysqlcommand,paramCommandConnection)上得到一个错误代码>无对象实例
。
我的错误在哪里mysqlconnection
不是null
——连接状态为open
解决方案
OMG:我已经读了很多很多资料(和这一行)。。很多次-但看不到错误:(
改为
typeMySqlCommand.GetMethod("set_Connection", new Type[] {typeMySqlConnection});
typeMySqlCommand.GetMethod("set_Connection", new Type[] {typeMySqlConnection});
一切都很好
重新添加raiserle解决方案
改为
typeMySqlCommand.GetMethod("set_Connection", new Type[] {typeMySqlConnection});
typeMySqlCommand.GetMethod("set_Connection", new Type[] {typeMySqlConnection});