How can i use dapper to connect to a sqlite database?

南笙酒味 提交于 2020-05-11 04:13:51

问题


How can I use dapper to connect and get data from a sqlite database?


回答1:


There is nothing magical you need to do. Just add:

using Dapper;

And run queries on your open SqliteConnection

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




回答2:


Here is a complete working example with an in-memory database. Requires 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);
        }
    }
}

Running the example:

$ dotnet run
3.30.1


来源:https://stackoverflow.com/questions/6995291/how-can-i-use-dapper-to-connect-to-a-sqlite-database

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!