Dapper - Async
ExecuteAsync
The can execute a command one or multiple times asynchronously and return the number of affected rows.
Try it: | .NET Framework
The QueryAsync
can execute a query and map the result asynchronously.
string sql = "SELECT TOP 10 * FROM OrderDetails";
using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
{
var orderDetails = connection.QueryAsync<OrderDetail>(sql).Result.ToList();
Console.WriteLine(orderDetails.Count());
FiddleHelper.WriteTable(orderDetails);
QueryFirstAsync
The QueryFirstAsync
can execute a query and map asynchronously the first result.
Try it: .NET Core |
The QueryFirstOrDefaultAsync
can execute a query and map asynchronously the first result, or a default value if the sequence contains no elements.
string sql = "SELECT * FROM OrderDetails WHERE OrderDetailID = @OrderDetailID;";
{
var orderDetail = connection.QueryFirstOrDefaultAsync<OrderDetail>(sql, new {OrderDetailID = 1}).Result;
FiddleHelper.WriteTable(new List<OrderDetail>() { orderDetail } );
}
Try it: .NET Core |
QuerySingleAsync
Try it: | .NET Framework
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.
string sql = "SELECT * FROM OrderDetails WHERE OrderDetailID = @OrderDetailID;";
using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
{
var orderDetail = connection.QuerySingleOrDefaultAsync<OrderDetail>(sql, new {OrderDetailID = 1}).Result;
FiddleHelper.WriteTable(new List<OrderDetail>() { orderDetail } );
}
Try it: | .NET Framework
QueryMultipleAsync
The QuerySingleOrDefaultAsync
can execute multiple queries within the same command and map results asynchronously.