How can I use dapper to connect and get data from 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!
You can compile the SQLite library or its CLI shell in Visual Studio, as well as applications that use the library.
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.
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")
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With