c# - MySql.Data via Reflection -
i use eplan scripting , want connect database. scripting engine allows load assemblies @ 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 not null object[] paramcommandtext = new object[] { "select * `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 error on methodmysqlcommand_connection.invoke (omysqlcommand, paramcommandconnection);
no instance of object
. error? omysqlconnection
not null
- , connection state says open
.
the solution
omg: i've read source (and line) many, many .. many times - can't see error :( !
typemysqlcommand.getmethod("set_connection", new type[] {typemysqlcommand});
change to
typemysqlcommand.getmethod("set_connection", new type[] {typemysqlconnection});
and works fine!
regrads raiserle
Comments
Post a Comment