The method QueryUnbufferedAsync has been added in the v2.0.138 to cover a very old mistake that requires breaking changes. The QueryAsync extends the IDbConnection however the IDbConnection class doesn't have the async method as NickCraver commented here.
The Async method should extend the DbConnection and not the IDbConnection, which will be done when the next major v3 version is released.
Dapper Query Unbuffered Async
The method QueryUnbufferedAsync has 2 main purposes:
- Selecting multiple data from your database asynchronously
- Loading data retrieved on demand (similar to
buffered = false)
In this example, we will start by using Dapper Plus to make the setup easier by using the CreateTable method and BulkInsert method. Then, we’ll show how to use the QueryUnbufferedAsync method from Dapper in a foreach statement using the await keyword and ConfigureAwait method:
language-csharp
|
public static async Task Main()
{
var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServer());
// CREATE database and data seeding
{
// CREATE Table (from Dapper Plus)
connection.CreateTable<Product>();
var seedProducts = new List<Product>();
seedProducts.Add(new Product() { Name = "Dapper Plus", CategoryID = 1, Description = @"Use <a href=""https://dapper-plus.net/"" target=""_blank"">Dapper Plus</a> to extend your IDbConnection with high-performance bulk operations." });
seedProducts.Add(new Product() { Name = "C# Eval Expression", CategoryID = 1, Description = @"Use <a href=""https://eval-expression.net/"" target=""_blank"">C# Eval Expression</a> to compile and execute C# code at runtime." });
seedProducts.Add(new Product() { Name = "Entity Framework Extensions", CategoryID = 2, Description = @"Use <a href=""https://entityframework-extensions.net/"" target=""_blank"">Entity Framework Extensions</a> to extend your DbContext with high-performance bulk operations." });
// BulkInsert data (from Dapper Plus)
connection.BulkInsert(seedProducts);
}
// Query data with Dapper
var sql = "SELECT * FROM Product WHERE CategoryID = @categoryID";
await foreach (var product in connection.QueryUnbufferedAsync<Product>(sql, new { categoryID = 1 }).ConfigureAwait(false))
{
Console.WriteLine($"ProductID: {product.ProductID}; Name: {product.Name}");
}
}