Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

sqlite

dapper

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

like image 589
David Avatar asked Aug 09 '11 11:08

David


People also ask

How do I connect to a SQLite database?

Select SQLite from the list. Give a Connection name for your own internal reference. For Database , click Choose a File and then select the database file on your local machine to which you want to connect. Hit Connect and you're all set!

Does Visual Studio support SQLite?

You can compile the SQLite library or its CLI shell in Visual Studio, as well as applications that use the library.

How do I import SQLite database into Visual Studio?

Getting Started with SQLite from a .Open Visual Studio, select new project, and, in Visual C#, select “Console Application” and provide the name as SQLiteDemo. Click OK. To connect SQLite with C#, we need drivers. Install all required SQLite resources from the NuGet package, as pictured in Figure 1.


2 Answers

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")

like image 184
Sam Saffron Avatar answered Sep 28 '22 03:09

Sam Saffron


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
like image 21
Jan Bodnar Avatar answered Sep 28 '22 05:09

Jan Bodnar