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

Dav*_*vid 12 sqlite dapper

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

Sam*_*ron 23

你不需要做任何神奇的事情.只需添加:

using Dapper;

并在您打开时运行查询 SqliteConnection

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


Jan*_*nar 10

这是一个带有内存数据库的完整工作示例。需要 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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

运行示例:

$ dotnet run
3.30.1
Run Code Online (Sandbox Code Playgroud)