我使用EPLAN脚本,我想连接一个数据库。脚本引擎允许在运行时加载程序集。
我的尝试:
Assembly assMySqlData = Assembly.LoadFrom(@"c:Program Files (x86)MySQLMySQL Connector Net 6.9.6Assembliesv4.0MySql.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");
}
}
我在no instance of object
methodMysqlCommand_Connection.Invoke (oMysqlCommand, paramCommandConnection);
时遇到错误.我的错误在哪里? oMySqlConnection
不是null
- 连接状态显示open
.
解决方案
OMG:我已经阅读了这个来源(和这一行)很多很多次 - 但看不到错误:(!
typeMySqlCommand.GetMethod("set_Connection", new Type[] {typeMySqlCommand});
更改为
typeMySqlCommand.GetMethod("set_Connection", new Type[] {typeMySqlConnection});
一切正常!
Regrads Raiserle
解决方案
typeMySqlCommand.GetMethod("set_Connection", new Type[] {typeMySqlCommand});
更改为
typeMySqlCommand.GetMethod("set_Connection", new Type[] {typeMySqlConnection});