加载SQLite DLL运行时

时间:2017-11-23 09:09:12

标签: c#

我需要在运行时使用SQLite dll。我得到如下的汇编:

Assembly a = Assembly.Load("System.Data.SQLite");

然后,创建对象SQLiteConnection类并向他添加ConnectionString属性,如:

object sq_connection = a.CreateInstance("System.Data.SQLite.SQLiteConnection");
sq_connection.GetType().GetProperty("ConnectionString").SetValue(sq_connection, "Con...");

但是,我需要使用SQLiteCommandCommandText属性创建ConnectionString对象:

 object sq_command = a.CreateInstance("System.Data.SQLite.SQLiteCommand");
 sq_command.GetType().GetProperty("CommandText").SetValue(sq_command, sql);
 sq_command.GetType().GetProperty("Connection").SetValue(sq_command, sq_connection);

CommandText"Connection"属性VS AmbiguousMatchException就可以了。我该如何解决?

1 个答案:

答案 0 :(得分:1)

这不是你问题的直接答案,但我认为你过于复杂了。您只需要一行就可以让所有数据库调用都与SQLite一起使用:创建数据库。其他所有内容都由编译时已知的接口处理:

using(var sq_connection = (IDbConnection)a.CreateInstance("System.Data.SQLite.SQLiteConnection"))
{
    sq_connection.ConnectionString = "Con...";
    using(var sq_command = sq_connection.CreateCommand())
    {
        sq_command.CommandText = sql;
        // execute
    }
}
相关问题