七天.NET 8操作SQLite入门到实战 - 第五天引入SQLite-net ORM并封装常用方法

318 篇文章 0 订阅

前言

上一章节我们搭建好了EasySQLite的前后端框架,今天我们的主要任务是在后端框架中引入SQLite-net ORM并封装常用方法(SQLiteHelper)。

七天.NET 8操作SQLite入门到实战详细教程

EasySQLite项目源码地址

  • GitHub地址:https://github.com/YSGStudyHards/EasySQLite[1]

SQLite-net介绍

简单、强大、跨平台的 SQLite 客户端和 .NET 的 ORM。

  • GitHub开源地址:https://github.com/praeclarum/sqlite-net

SQLite-net提供了以下四个包:

PackageDescriptionPackage Address
sqlite-net-pcl.NET Standard 库https://www.nuget.org/packages/sqlite-net-pcl
sqlite-net-sqlcipher支持加密https://www.nuget.org/packages/sqlite-net-sqlcipher
sqlite-net-static使用平台提供的 sqlite3 的 P/Invokes 的特殊版本https://www.nuget.org/packages/sqlite-net-static
sqlite-net-base使用 SQLitePCLRaw 捆绑包,以便您可以选择自己的提供程序https://www.nuget.org/packages/sqlite-net-base

SQLite-net 设计目标

SQLite-net 被设计为一个快速便捷的数据库层。其设计遵循以下目标:

  • 非常容易与现有项目集成,并在所有 .NET 平台上运行。

  • 对 SQLite 的薄包装,快速高效。(这个库不应该成为查询性能的瓶颈。)

  • 提供非常简单的方法来安全执行 CRUD 操作和查询(使用参数),以及以强类型方式检索这些查询结果。

  • 在不强制更改类的情况下与数据模型一起工作。(包含一个小型的反射驱动 ORM 层。)

安装 sqlite-net-pcl  Nuget包

图片

搜索sqlite-net-pcl,选择最新稳定版本进行安装:

图片

SQLite同步和异步方法帮助类

SQLiteHelper

    /// <summary>
    /// SQLite同步方法帮助类
    /// 作者:追逐时光者
    /// 创建时间:2023年11月30日
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class SQLiteHelper<T> where T : new()
    {
        private readonly string _databasePath = Path.Combine(Environment.CurrentDirectory, "ClassManagement.db");
        private readonly SQLiteConnection _connection; // SQLite连接对象

        /// <summary>
        /// 构造函数
        /// </summary>
        public SQLiteHelper()
        {
            // 创建SQLite连接对象并打开连接
            _connection = new SQLiteConnection(_databasePath);
            _connection.CreateTable<T>(); // 如果表不存在,则创建该表[不会创建重复的表]
        }

        /// <summary>
        /// 数据插入
        /// </summary>
        /// <param name="item">要插入的数据项</param>
        /// <returns></returns>
        public int Insert(T item)
        {
            return _connection.Insert(item);
        }

        /// <summary>
        /// 数据删除
        /// </summary>
        /// <param name="id">要删除的数据的主键ID</param>
        /// <returns></returns>
        public int Delete(int id)
        {
            return _connection.Delete<T>(id);
        }

        /// <summary>
        /// 数据更新
        /// </summary>
        /// <param name="item">要更新的数据项</param>
        /// <returns></returns>
        public int Update(T item)
        {
            return _connection.Update(item);
        }

        /// <summary>
        /// 根据条件查询记录
        /// </summary>
        /// <param name="predExpr">查询条件</param>
        /// <returns></returns>
        public List<T> Query(Expression<Func<T, bool>> predExpr)
        {
            return _connection.Table<T>().Where(predExpr).ToList();
        }

        /// <summary>
        /// 查询所有数据
        /// </summary>
        /// <returns></returns>
        public List<T> QueryAll()
        {
            return _connection.Table<T>().ToList();
        }

        /// <summary>
        /// 根据条件查询单条记录
        /// </summary>
        /// <param name="predExpr">查询条件</param>
        /// <returns></returns>
        public T QuerySingle(Expression<Func<T, bool>> predExpr)
        {
            return _connection.Table<T>().Where(predExpr).FirstOrDefault();
        }
    }

SQLiteAsyncHelper

    /// <summary>
    /// SQLite异步方法帮助类
    /// 作者:追逐时光者
    /// 创建时间:2023年11月30日
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class SQLiteAsyncHelper<T> where T : new()
    {
        private readonly string _databasePath = Path.Combine(Environment.CurrentDirectory, "ClassManagement.db");
        private readonly SQLiteAsyncConnection _connectionAsync; // SQLite连接对象

        /// <summary>
        /// 构造函数
        /// </summary>
        public SQLiteAsyncHelper()
        {
            // 创建SQLite连接对象并打开连接
            _connectionAsync = new SQLiteAsyncConnection(_databasePath);
            _connectionAsync.CreateTableAsync<T>(); // 如果表不存在,则创建该表[不会创建重复的表]
        }

        /// <summary>
        /// 数据插入
        /// </summary>
        /// <param name="item">要插入的数据项</param>
        /// <returns></returns>
        public async Task<int> InsertAsync(T item)
        {
            return await _connectionAsync.InsertAsync(item);
        }

        /// <summary>
        /// 数据删除
        /// </summary>
        /// <param name="id">要删除的数据的主键ID</param>
        /// <returns></returns>
        public async Task<int> DeleteAsync(int id)
        {
            return await _connectionAsync.DeleteAsync<T>(id);
        }

        /// <summary>
        /// 数据更新
        /// </summary>
        /// <param name="item">要更新的数据项</param>
        /// <returns></returns>
        public async Task<int> UpdateAsync(T item)
        {
            return await _connectionAsync.UpdateAsync(item);
        }

        /// <summary>
        /// 根据条件查询记录
        /// </summary>
        /// <param name="predExpr">查询条件</param>
        /// <returns></returns>
        public async Task<List<T>> QueryAsync(Expression<Func<T, bool>> predExpr)
        {
            return await _connectionAsync.Table<T>().Where(predExpr).ToListAsync();
        }

        /// <summary>
        /// 查询所有数据
        /// </summary>
        /// <returns></returns>
        public async Task<List<T>> QueryAllAsync()
        {
            return await _connectionAsync.Table<T>().ToListAsync();
        }

        /// <summary>
        /// 根据条件查询单条记录
        /// </summary>
        /// <param name="predExpr">查询条件</param>
        /// <returns></returns>
        public async Task<T> QuerySingleAsync(Expression<Func<T, bool>> predExpr)
        {
            return await _connectionAsync.Table<T>().Where(predExpr).FirstOrDefaultAsync();
        }
    }

参考资料

[1]

https://github.com/YSGStudyHards/EasySQLite: https://github.com/YSGStudyHards/EasySQLite

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

追逐时光者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值