Dapper queries in C#
SQL in, typed objects out, nothing in between.
using Dapper;
using Microsoft.Data.Sqlite;
using var db = new SqliteConnection("Data Source=:memory:");
db.Execute("CREATE TABLE tools(name TEXT, stars INT)");
db.Execute("INSERT INTO tools VALUES ('dapper', 17000), ('polly', 13000)");
// Dapper maps rows straight onto your type, no ORM ceremony
var tools = db.Query<Tool>("SELECT name, stars FROM tools ORDER BY stars");
foreach (var tool in tools)
{
Console.WriteLine($"{tool.Name}: {tool.Stars}");
}
record Tool(string Name, int Stars);
How it works
Executeruns DDL and inserts on any ADO.NET connection.Query<Tool>maps columns onto the record by name.- The SQL stays visible — Dapper adds mapping, not abstraction.
Keywords and builtins used here
Toolforeachinintnewrecordstringusingvar
The run, in numbers
- Lines
- 15
- Characters to type
- 493
- Tokens
- 70
- Three-star pace
- 85 tpm
At the three-star pace of 85 tokens a minute, this run takes about 49 seconds.
Step 1 of 2 in Data with Dapper, step 4 of 16 in The open ecosystem.