Introduction & Installation

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:

  1. Does not wrap SQL; developers write SQL statements manually;
  2. Extends IDbConnection via extension methods (usable with any database connection object);
  3. 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 IDbConnection interface, 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 install Microsoft.Data.SqlClient separately; for MySQL install MySqlConnector.

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

FrameworkTypeSQL ControlUse‑Cases
DapperMicro ORMManual SQL writingHigh‑performance requirements, complex queries, legacy project modernization
EF CoreFull ORMAuto‑generated SQL, raw SQL also supportedRapid development, simple business logic, avoid manual SQL

Introduction & Installation

Leave a Reply

Your email address will not be published. Required fields are marked *