Async

Description

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

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

ExecuteAsync

Example
string sql = "INSERT INTO Customers (CustomerName) Values (@CustomerName);";

using (var connection = new SqlCeConnection("Data Source=SqlCe_W3Schools.sdf"))
{
	connection.Open();
	var affectedRows = connection.ExecuteAsync(sql, new {CustomerName = "Mark"}).Result;
}

Try it online

QueryAsync

Example
string sql = "SELECT * FROM OrderDetails";

using (var connection = new SqlCeConnection("Data Source=SqlCe_W3Schools.sdf"))
{
	connection.Open();
	
	var orderDetails = connection.QueryAsync(sql).Result.ToList();

	Console.WriteLine(orderDetails.Count());
}

Try it online

QueryFirstAsync

Example
string sql = "SELECT * FROM OrderDetails WHERE OrderDetailID = @OrderDetailID;";

using (var connection = new SqlCeConnection("Data Source=SqlCe_W3Schools.sdf"))
{
	connection.Open();
	
	var orderDetail = connection.QueryFirstAsync<OrderDetail>(sql, new {OrderDetailID = 1}).Result;

	Console.WriteLine(orderDetail);
}

Try it online

QueryFirstOrDefaultAsync

Example
string sql = "SELECT * FROM OrderDetails WHERE OrderDetailID = @OrderDetailID;";

using (var connection = new SqlCeConnection("Data Source=SqlCe_W3Schools.sdf"))
{
	connection.Open();
	
	var orderDetail = connection.QueryFirstOrDefaultAsync<OrderDetail>(sql, new {OrderDetailID = 1}).Result;

	Console.WriteLine(orderDetail.Quantity);
}

Try it online

QuerySingleAsync

Example
string sql = "SELECT * FROM OrderDetails WHERE OrderDetailID = @OrderDetailID;";

using (var connection = new SqlCeConnection("Data Source=SqlCe_W3Schools.sdf"))
{
	connection.Open();
	
	var orderDetail = connection.QuerySingleAsync<OrderDetail>(sql, new {OrderDetailID = 1}).Result;

	Console.WriteLine(orderDetail.OrderDetailID);
}

Try it online

QuerySingleOrDefaultAsync

Example
string sql = "SELECT * FROM OrderDetails WHERE OrderDetailID = @OrderDetailID;";

using (var connection = new SqlCeConnection("Data Source=SqlCe_W3Schools.sdf"))
{
	connection.Open();
	
	var orderDetail = connection.QuerySingleOrDefaultAsync<OrderDetail>(sql, new {OrderDetailID = 1}).Result;

	Console.WriteLine(orderDetail.OrderDetailID);
}

Try it online

QueryMultipleAsync

Example
var sql = "SELECT * FROM Invoice; SELECT * FROM InvoiceItem;";

using (var connection = My.ConnectionFactory())
{
	connection.Open();

	using (var multi = connection.QueryMultipleAsync(sql, new { InvoiceID = 1 }).Result)
	{
		var invoice = multi.Read<Invoice>().First();
		var invoiceItems = multi.Read<InvoiceItem>().ToList();
	}
}