【问题标题】:How do I access SQLite database directly from DLL (without using app.config)?如何直接从 DLL 访问 SQLite 数据库(不使用 app.config)?
【发布时间】:2019-02-08 22:50:26
【问题描述】:

我正在尝试从 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 提供程序已安装或注册在应用程序配置中。

【问题讨论】:

    标签: c# .net entity-framework sqlite


    【解决方案1】:
    using (SQLiteConnection connection = new SQLiteConnection(string.Format("Data Source={0};Version=3;", <your_database_file_path>)))
    {
    
    }
    

    【讨论】:

    • 虽然这段代码可能有助于解决这个问题,但最好包含一些上下文,解释它是如何工作的以及何时使用它。从长远来看,纯代码的答案往往不太有用。有关更多信息,请参阅How do I write a good answer?
    猜你喜欢
    • 2019-12-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-12
    • 2016-12-10
    • 1970-01-01
    • 2018-03-25
    • 2012-03-16
    • 1970-01-01
    相关资源
    最近更新 更多