GitHub

title Dapper Insert, Insert Async, Multiple Rows, Bulk Insert
description The Dapper Execute method allows you to insert single data or insert multiple rows. In addition, you can use Dapper Plus to BulkInsert data in your database.
canonical /saving-data/insert
status Published
lastmod 2025-07-10

Inserting Data With Dapper

To insert data in Dapper, you need to write your INSERT SQL statement and call the Execute method. Make sure to use parameters when passing your values to avoid SQL Injection.

Dapper Insert

To insert in Dapper, you need to use the Execute method with an INSERT statement and provide your query parameter values.

In this example:

  1. We will create a connection
  2. We will create an INSERT SQL statement
  3. Call the Execute method
  • 3a. The first time, we will pass parameter values with an anonymous type
  • 3b. The second time, we will pass parameter values by providing the customer entity
// 1. We will create a connection
using (var connection = new SqlConnection(connectionString))
{
    // 2. We will create an `INSERT` SQL statement
	var sql = "INSERT INTO Customers (Name, Email) VALUES (@Name, @Email)";
	// 3. Call the `Execute` method
	{
		// 3a. The first time, we will pass parameter values with an anonymous type
		var anonymousCustomer = new { Name = "ZZZ Projects", Email = "zzzprojects@example.com" };
		var rowsAffected = connection.Execute(sql, anonymousCustomer);
		Console.WriteLine($"{rowsAffected} row(s) inserted.");
	}
	{
		// 3b. The second time, we will pass parameter values by providing the customer entity
		var customer = new Customer() { Name = "Learn Dapper", Email = "learndapper@example.com" };
		var rowsAffected = connection.Execute(sql, customer);
		Console.WriteLine($"{rowsAffected} row(s) inserted.");
	}
	var insertedCustomers = connection.Query<Customer>("SELECT * FROM Customers").ToList();
}

Dapper Insert Async

To insert in Dapper asynchronously, you need to use the ExecuteAsync method. Unfortunately, Dapper doesn't support cancellation token.

In this example, we will use the same logic as the previous example with a few differences:

  • Replace the Execute method with the ExecuteAsync method
  • Use the await keyword
  • The ConfigureAwait(false) part is optional (depending on your application type)
// 1. We will create a connection
using (var connection = new SqlConnection(connectionString))
{
    // 2. We will create an `INSERT` SQL statement
	var sql = "INSERT INTO Customers (Name, Email) VALUES (@Name, @Email)";
	// 3. Call the `ExecuteAsync` method
	{
		// 3a. The first time, we will pass parameter values with an anonymous type
		var anonymousCustomer = new { Name = "ZZZ Projects", Email = "zzzprojects@example.com" };
		var rowsAffected = await connection.ExecuteAsync(sql, anonymousCustomer);
		// or var rowsAffected = await connection.ExecuteAsync(sql, anonymousCustomer).ConfigureAwait(false);
		Console.WriteLine($"{rowsAffected} row(s) inserted.");
	}
	{
		// 3b. The second time, we will pass parameter values by providing the entity
		var customer = new Customer() { Name = "Learn Dapper", Email = "learndapper@example.com" };
		var rowsAffected = await connection.ExecuteAsync(sql, customer);
		// or var rowsAffected = await connection.ExecuteAsync(sql, customer).ConfigureAwait(false);
		Console.WriteLine($"{rowsAffected} row(s) inserted.");
	}
	var insertedCustomers = connection.Query<Customer>("SELECT * FROM Customers").ToList();
}

Dapper Insert Multiple Rows

For inserting multiple rows in Dapper, you have to provide a list to the Execute or ExecuteAsync method instead of a single anonymous object or an entity.

:::{.alert .alert-warning} NOTE: Passing a list doesn't insert rows in bulk. Every item will be inserted by using a single insert statement (similar to looping on your item list to call the Execute method) :::

In this example:

  1. We will create a connection
  2. We will create an INSERT SQL statement
  3. Call the Execute method 3a. The first time, we will pass parameter values with a list of anonymous objects 3b. The second time, we will pass parameter values by providing a list of customers
"// 1. We will create a connection using (var connection = new SqlConnection(connectionString)) { // 2. We will create an `INSERT` SQL statement var sql = "INSERT INTO Customers (Name, Email) VALUES (@Name, @Email)"; // 3. Call the `Execute` method { // 3a. The first time, we will pass parameter values with a list of anonymous objects var anonymousCustomers = new List

Read the original on github.com ↗