Skip to content

Instantly share code, notes, and snippets.

@martinandersen3d
Forked from fravelgue/00-Using_SQLite.dib
Created November 21, 2022 22:47
Show Gist options
  • Save martinandersen3d/40eac9bc11a2a129ee31802e636868d3 to your computer and use it in GitHub Desktop.
Save martinandersen3d/40eac9bc11a2a129ee31802e636868d3 to your computer and use it in GitHub Desktop.
Interactive (Polyglot) Notebook using SQLite
#!markdown
# Using SQLite in Notebook
Install ExtensionLab NuGet package
#!csharp
#r "nuget: System.Data.SQLite, *-*"
#r "nuget: Microsoft.DotNet.Interactive.ExtensionLab, *-*"
#r "nuget: Dapper, *-*"
#!csharp
#!lsmagic
#!markdown
Installing the `ExtensionLab` brings in some new magic commands including two additional verbs for the `#!connect` command, which you can see by running help for it.
#!csharp
#!connect -h
#!csharp
#!connect sqlite -h
#!markdown
## Using SQLite in SQL Notebook
This sample demonstrates how to use the `#!connect` extension
#!csharp
#!connect sqlite --kernel-name hellodb "Data Source=helloDB;Mode=Memory;Cache=Shared"
#!sql
#!sql-hellodb
select 10;
#!markdown
## Using Dapper and SQLite in C# Notebook
#!csharp
using System.Data.SQLite;
using Dapper;
using (var conn = new SQLiteConnection("Data Source=helloDB;Mode=Memory;Cache=Shared"))
{
var res = conn.Query<int>("SELECT 50").FirstOrDefault();
Console.WriteLine($"Result: {res}");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment