1. What Is Dapper
Dapper is a .NET Micro ORM, widely known as the “King of Micro ORMs”, delivering performance close to raw ADO.NET.
ORM: Object‑Relational Mapper, handles automatic mapping between database tables and C# entity objects.
Core features:
- Does not wrap SQL; developers write SQL statements manually;
- Extends
IDbConnectionvia extension methods (usable with any database connection object); - Lightweight footprint with excellent performance.
Note: Technically it is a Micro ORM, different from full‑featured ORM frameworks such as EF Core. There is ongoing community debate over whether it qualifies as a pure ORM.
Preventing SQL Injection: Use parameterized queries to avoid injection risks; concatenating raw SQL remains unsafe.
Three‑Step Workflow: Create database connection → Write SQL statement → Call Dapper extension method to execute SQL with parameters.
Features
- High performance and fast query execution
- Supports mapping to static entities and dynamic objects
- Full control over SQL statements
- Multi‑result‑set query support
- Native stored‑procedure support
- Built on the
IDbConnectioninterface, compatible with most database drivers - Bulk insert support (extended capability; basic native bulk operations. For large‑scale bulk operations, use Dapper.Plus)
Installation
Option 1: NuGet Package Manager Console (PMC)
Install-Package Dapper
Option 2: .NET CLI (Recommended for .NET Core/.NET5+)
dotnet add package Dapper
Option 3: Visual Studio GUI
Solution Explorer → Right‑click your project → Manage NuGet Packages, search for Dapper and install it.
Important note: Dapper does not ship with database drivers!
For SQL Server installMicrosoft.Data.SqlClientseparately; for MySQL installMySqlConnector.
Example
using Dapper;
using Microsoft.Data.SqlClient;
// 1. Initialize IDbConnection
string connStr = "Your database connection string";
using var conn = new SqlConnection(connStr);
// 2. Define SQL
string sql = "SELECT * FROM Article WHERE Id = @Id";
// 3. Execute query and auto‑map to entity
var article = conn.QueryFirstOrDefault<Article>(sql, new { Id = 1 });
public class Article
{
public int Id { get; set; }
public string Title { get; set; }
}Code language: JavaScript (javascript)
Comparison
| Framework | Type | SQL Control | Use‑Cases |
|---|---|---|---|
| Dapper | Micro ORM | Manual SQL writing | High‑performance requirements, complex queries, legacy project modernization |
| EF Core | Full ORM | Auto‑generated SQL, raw SQL also supported | Rapid development, simple business logic, avoid manual SQL |
Introduction & Installation