我如何使用dapper连接到sqlite数据库



如何使用dapper连接并从sqlite数据库获取数据?

你不需要做什么神奇的事情。只需添加:

using Dapper;

在open SqliteConnection上运行查询

cnn.Query("select 'hello world' from Table")

下面是一个使用内存中数据库的完整工作示例。需要c# 8.0.

using System;
using System.Data.SQLite;
using Dapper;
namespace First
{
    // dotnet add package System.Data.SQLite.Core
    // dotnet add package Dapper
    class Program
    {
        static void Main(string[] args)
        {
            string cs = "Data Source=:memory:";
            using var con = new SQLiteConnection(cs);
            con.Open();
            var res = con.QueryFirst("select SQLITE_VERSION() AS Version");
            Console.WriteLine(res.Version);
        }
    }
}

运行示例:

$ dotnet run
3.30.1

最新更新