【问题标题】:How can I use Sqlite in a .NET Standard library?如何在 .NET Standard 库中使用 Sqlite?
【发布时间】:2021-07-05 20:28:16
【问题描述】:

有谁知道如何在 .NET Standard 库中设置 sqlite? SQLite.Net-PCL 似乎不兼容,或者至少 nuget 是这样告诉我的。

【问题讨论】:

  • 您可以将 Microsoft Entityframework Core 与 .NET Standard 库一起使用。当然,它比 SQLite.NET-PCL 要重得多。但是,我没有发现任何迹象表明维护人员会继续支持 Netstandard。

标签: .net sqlite .net-standard


【解决方案1】:

是的,这是 SQLite 包装器的 .NET Standard 实现:

https://github.com/MelbourneDeveloper/SQLite.Net.Standard.git

安装包 SQLite.Net.Standard

它非常简单,只有一个 DLL 可以在所有平台上运行。它还针对 PCL。

【讨论】:

  • 不错!这周我会看看它并回复你。谢谢!
  • 不用担心。这个库目前没有实现 Oystern Krog 在他的版本中具有的类似 ORM 的功能。我一直在考虑是否要在顶层导入他的代码,或者为 SQLite 实现类似 ADO 的东西。 ADO 是老派,但对遗留代码兼容性很有用。
【解决方案2】:

这是完整的示例。纯.Net标准

Nuget 包 => sqlite-net-pcl

https://www.nuget.org/packages/sqlite-net-pcl/1.7.335?_src=template

Nuget 包 => System.Data.SQLite https://www.nuget.org/packages/System.Data.SQLite/1.0.114?_src=template

或者来自微软的总是更新的sqlite

https://www.nuget.org/packages/Microsoft.Data.Sqlite

using DB.Extensions;
using DB.Modellers;
using SQLite;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace DB
{
    public class DatabaseAdapterStandard
    {
        
        public string SqlFilePath { get; }
        public DatabaseAdapterStandard(string sqlFilePath)
        {
            this.SqlFilePath = sqlFilePath;
          
        }
       
      
        public IEnumerable<books> GetBooks()
        {
            using (var session = new SQLiteConnection(SqlFilePath))
            {
                var books = session.Table<books>().ToList();
                return books;
            }
        }
        public IEnumerable<chapters> GetChapters(books books)
        {
            using (var session = new SQLiteConnection(SqlFilePath))
            {
                var items = session.Table<chapters>().Where(p=>p.reference_human == books.human).ToList();
                return items;
            }
        }
        public IEnumerable<verses> GetVerses()
        {
            using (var session = new SQLiteConnection(SqlFilePath))
            {
                var items = session.Table<verses>().ToList();
                return items;
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 2018-06-21
    • 2019-07-24
    • 1970-01-01
    • 1970-01-01
    • 2021-05-29
    • 1970-01-01
    • 2017-05-10
    • 2018-07-06
    相关资源
    最近更新 更多