19

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

4

2 回答 2

28

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

using Dapper;

并在您的公开场合运行查询SqliteConnection

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

于 2011-08-11T07:37:07.593 回答
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);
        }
    }
}

运行示例:

$ dotnet run
3.30.1
于 2019-12-15T14:08:46.067 回答