Dapper - Async

Description

Dapper also extend the IDbConnection interface with Async (asynchronous) methods:

We only added a non-asynchronous version in this tutorial to make it easier to read.

ExecuteAsync

The ExecuteAsync can execute a command one or multiple times asynchronously and return the number of affected rows.

  1. string sql = "INSERT INTO Customers (CustomerName) Values (@CustomerName);";
  2. using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
  3. {
  4. var affectedRows = connection.ExecuteAsync(sql, new {CustomerName = "Mark"}).Result;
  5. Console.WriteLine(affectedRows);
  6. var customer = connection.Query<Customer>("Select * FROM CUSTOMERS WHERE CustomerName = 'Mark'").ToList();
  7. FiddleHelper.WriteTable(customer);
  8. }

Try it: .NET Core | .NET Framework

QueryAsync

The QueryAsync can execute a query and map the result asynchronously.

  1. string sql = "SELECT TOP 10 * FROM OrderDetails";
  2. using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
  3. {
  4. var orderDetails = connection.QueryAsync<OrderDetail>(sql).Result.ToList();
  5. Console.WriteLine(orderDetails.Count());
  6. FiddleHelper.WriteTable(orderDetails);
  7. }

Try it: .NET Core | .NET Framework

QueryFirstAsync

The QueryFirstAsync can execute a query and map asynchronously the first result.

  1. string sql = "SELECT * FROM OrderDetails WHERE OrderDetailID = @OrderDetailID;";
  2. using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
  3. {
  4. var orderDetail = connection.QueryFirstAsync<OrderDetail>(sql, new {OrderDetailID = 1}).Result;
  5. FiddleHelper.WriteTable(new List<OrderDetail>() { orderDetail } );
  6. }

Try it: .NET Core | .NET Framework

QueryFirstOrDefaultAsync

The QueryFirstOrDefaultAsync can execute a query and map asynchronously the first result, or a default value if the sequence contains no elements.

  1. string sql = "SELECT * FROM OrderDetails WHERE OrderDetailID = @OrderDetailID;";
  2. using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
  3. {
  4. var orderDetail = connection.QueryFirstOrDefaultAsync<OrderDetail>(sql, new {OrderDetailID = 1}).Result;
  5. FiddleHelper.WriteTable(new List<OrderDetail>() { orderDetail } );
  6. }

Try it: .NET Core | .NET Framework

QuerySingleAsync

The QuerySingleAsync can execute a query and map asynchronously the first result and throws an exception if there is not exactly one element in the sequence.

  1. string sql = "SELECT * FROM OrderDetails WHERE OrderDetailID = @OrderDetailID;";
  2. using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
  3. {
  4. var orderDetail = connection.QuerySingleOrDefaultAsync<OrderDetail>(sql, new {OrderDetailID = 1}).Result;
  5. FiddleHelper.WriteTable(new List<OrderDetail>() { orderDetail } );
  6. }

Try it: .NET Core | .NET Framework

QuerySingleOrDefaultAsync

The QuerySingleOrDefaultAsync can execute a query and map asynchronously the first result, or a default value if the sequence is empty; this method throws an exception if there is more than one element in the sequence.

  1. string sql = "SELECT * FROM OrderDetails WHERE OrderDetailID = @OrderDetailID;";
  2. using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
  3. {
  4. var orderDetail = connection.QuerySingleOrDefaultAsync<OrderDetail>(sql, new {OrderDetailID = 1}).Result;
  5. FiddleHelper.WriteTable(new List<OrderDetail>() { orderDetail } );
  6. }

Try it: .NET Core | .NET Framework

QueryMultipleAsync

The QuerySingleOrDefaultAsync can execute multiple queries within the same command and map results asynchronously.

  1. var sql = "SELECT TOP 3 * FROM Orders; SELECT TOP 3 * FROM OrderDetails;";
  2. using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
  3. {
  4. connection.Open();
  5. using (var multi = connection.QueryMultipleAsync(sql).Result)
  6. {
  7. var orders = multi.Read<Order>().ToList();
  8. var orderDetails = multi.Read<OrderDetail>().ToList();
  9. FiddleHelper.WriteTable(orders);
  10. FiddleHelper.WriteTable(orderDetails);
  11. }
  12. }

Try it: .NET Core | .NET Framework