Dapper .NET Data Access APIs

Dapper is a popular, high-performance Object Relational Mapper (ORM) for .NET. It allows you to map query results directly to your Plain Old CLR Objects (POCOs) with minimal overhead. Dapper is known for its speed and simplicity, often outperforming other ORMs in benchmarks.

What is Dapper?

Dapper, often referred to as "the fastest micro-ORM," focuses on providing an efficient way to execute SQL queries and map the results to .NET objects. It achieves this by leveraging dynamic methods and generics, minimizing reflection and intermediate object creation.

Key Features

Getting Started with Dapper

To use Dapper, you first need to install it via NuGet. You'll also need a connection to your database. The core Dapper extension methods are defined on the IDbConnection interface.

Prerequisites

Core Concepts

Dapper's main purpose is to simplify the interaction between your .NET code and your database. The primary operations involve:

Example Usage

Here's a simple example of querying data using Dapper:


using System.Data;
using System.Data.SqlClient; // Or your specific database provider
using Dapper;
using System.Collections.Generic;

// Assume 'connectionString' is your database connection string
string connectionString = "Server=myServer;Database=myDatabase;Integrated Security=True;";

public class Product
{
    public int ProductId { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
}

public class ProductRepository
{
    public IEnumerable<Product> GetAllProducts()
    {
        using (IDbConnection db = new SqlConnection(connectionString))
        {
            string sql = "SELECT ProductId, Name, Price FROM Products";
            return db.Query<Product>(sql);
        }
    }
}
        

Key Dapper Extension Methods

Query<T>(string sql, object param = null, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = null)

Executes a query and returns an enumeration of typed results. This is the most common method for retrieving lists of data.

QueryFirstOrDefault<T>(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)

Executes a query and returns the first element of the result set, or a default value if the result set is empty.

QuerySingle<T>(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)

Executes a query and returns the first element of the result set. Throws an exception if the result set is empty or contains more than one element.

Execute(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)

Executes a command against the database and returns the number of rows affected.

Learn More

Explore the sections below to dive deeper into Dapper's capabilities, including installation, querying, executing commands, transactions, and advanced features.