I use EPLAN Scripting and I want to connect a database. The scripting engine allows to load assemblies at runtime.
My try:
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");
}
}
I get an error on methodMysqlCommand_Connection.Invoke (oMysqlCommand, paramCommandConnection);
with no instance of object
. Where is my error? oMySqlConnection
is not null
- and the connection state says open
.
Aucun commentaire:
Enregistrer un commentaire