Dapper for C# (.NET): Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction to Dapper: What It Is and Why You Need It

\n\n

Dapper is a lightweight library (Micro ORM) for the .NET platform, developed by the Stack Overflow team. It is an extension for IDbConnection and is designed to execute SQL queries against databases with minimal overhead. Unlike heavyweight ORMs (e.g., Entity Framework Core), Dapper does not track object changes, does not generate complex SQL queries, and does not require data context configuration. Its main goal is maximum performance and simplicity.

\n\n

Key advantages of Dapper:

\n
    \n
  • Speed: Dapper works almost as fast as raw ADO.NET commands because it uses dynamic IL code generation for result mapping.
  • \n
  • Simplicity: Minimal code required to execute queries. No need to create complex configurations.
  • \n
  • Control over SQL: You write SQL queries manually, giving you full control over what data is retrieved and how.
  • \n
  • Flexibility: Works with any ADO.NET provider (SQL Server, PostgreSQL, MySQL, SQLite, etc.).
  • \n
  • Extensibility: Easily integrates with existing projects and other ORMs.
  • \n
\n\n

Dapper is ideal for projects where performance and direct SQL control are important, as well as for microservices and high-load applications.

\n\n

Installing Dapper

\n\n

Dapper is distributed via NuGet, the package manager for .NET. It can be installed in several ways:

\n\n

Via Package Manager Console (Visual Studio):

\n
Install-Package Dapper
\n\n

Via .NET CLI (recommended method):

\n
dotnet add package Dapper
\n\n

Via NuGet interface in Visual Studio:

\n
    \n
  1. Right-click on the project → "Manage NuGet Packages".
  2. \n
  3. Go to the "Browse" tab.
  4. \n
  5. Find "Dapper" and click "Install".
  6. \n
\n\n

After installation, a reference to the Dapper.dll assembly will appear in the project. No additional dependencies are required — the library works on top of the standard System.Data.

\n\n

Quick Start with Code Example

\n\n

Let's look at a minimal example of working with Dapper. Suppose we have a Users table in a SQL Server database:

\n\n
CREATE TABLE Users (\n    Id INT PRIMARY KEY IDENTITY,\n    Name NVARCHAR(100) NOT NULL,\n    Age INT NOT NULL\n);
\n\n

C# code to retrieve all users:

\n\n
using System.Data.SqlClient;\nusing Dapper;\n\npublic class User\n{\n    public int Id { get; set; }\n    public string Name { get; set; }\n    public int Age { get; set; }\n}\n\nclass Program\n{\n    static void Main()\n    {\n        string connectionString = "Server=localhost;Database=TestDB;Trusted_Connection=True;";\n        using (var connection = new SqlConnection(connectionString))\n        {\n            connection.Open();\n            \n            // Simple SELECT query\n            var users = connection.Query<User>("SELECT * FROM Users");\n            \n            foreach (var user in users)\n            {\n                Console.WriteLine($"{user.Id}: {user.Name}, {user.Age} years old");\n            }\n        }\n    }\n}
\n\n

As you can see, Dapper automatically maps SQL query results to C# objects. The Query<T> method is the main method for executing SELECT queries.

\n\n

Main Dapper Methods/Functions

\n\n

Dapper provides a set of extension methods for the IDbConnection interface. Let's look at 10 key methods.

\n\n

1. Query<T>

\n

What it does: Executes a SQL query and returns a collection of objects of the specified type T. Used for SELECT queries that return multiple rows.

\n

Parameters:

Recommendations