如何直接从DLL访问SQLite数据库(不使用app.config)?

时间:2019-02-08 22:50:27

标签: c# .net entity-framework sqlite

我正在尝试连接到DLL并使用它的SQLite数据库文件。到目前为止,我有以下内容

        ASSEMBLY_PATH = ReturnThisAssemblyPath();

        DB_PATH = ASSEMBLY_PATH + @"\Datastore\" + DB_FILENAME;
        try
        {
            DbProviderFactory fac = System.Data.SQLite.SQLiteFactory.Instance;

            var conn = fac.CreateConnection();

            conn.ConnectionString = "data source =" + DB_PATH + @"\SQLiteDatabase.db";
            using (var m = new ReportsContext(conn))
            {
                try
                {
                    m.Reports.Add(new Datastore.Report() { reportTypeId = "1", reportType = "HL7", originalString = "orig10", processedString = "mod10" });
                    m.SaveChanges();
                }
                catch (System.Exception ex)
                {

                }
                var res = m.Reports.Find(111);
            }

        }catch(System.Exception ex)
        {

        }

我也尝试过DbProviderFactory fac = System.Data.SQLite.EF6.SQLiteProviderFactory.Instance;

但是当尝试添加记录时,它失败并显示以下错误

  

无法确定类型的提供程序工厂的提供程序名称   'System.Data.SQLite.SQLiteFactory'。确保ADO.NET   提供程序已安装或在应用程序配置中注册。

1 个答案:

答案 0 :(得分:0)

using (SQLiteConnection connection = new SQLiteConnection(string.Format("Data Source={0};Version=3;", <your_database_file_path>)))
{

}