C# 使用dapper操作 SQLite3 的示例

sqllite3 的 “安装”不用说了,直接下载就用。
可以用Navicat for SQLite 当作客户端连接此数据库,直接操作。

上一个控制台代码,查询的,其他也一样。dapper操作。

using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Linq;

using Dapper;


using SQLiteDemo.Model;

namespace SQLiteDemo
{
    public class SQLiteTool
    {
        public static string conn = @"Data Source=D:\green\sqlite\test.db;";

        public static List<Student> GetAllStudents()
        {
           
            using (IDbConnection cnn = new SQLiteConnection(conn))
            {
                cnn.Open();

                var output = cnn.Query<Student>("select * from student").ToList();

                return output.ToList();
            }
        }
    }
}

此处需要特别注意,这个 SQLiteConnection 是 System.Data.SQLite.SQLiteConnection, 不是 Microsoft.Data.Sqlite.SqliteConnection
否则cnn.Open() 会报错。
报错内容:

You need to call SQLitePCL.raw.SetProvider(). If you are using a bundle package, this is done by calling SQLitePCL.Batteries.Init().”


上个Nuget依赖图:
在这里插入图片描述
上图中,红框中的那个不需要,可删除,会造成迷惑。
在这里插入图片描述
上图中即为必要的,当然你可以可以不用NewtonSoft.Json

完毕,其余跟dapper操作mysql什么的就一样了。


上个客户端图:

在这里插入图片描述


上个git地址: https://gitee.com/festone/dot-net5-dapper-sqlite3

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值