This tutorial will show you how to use async with Dapper ORM in c# code. Basically, Dapper provides the convenience of mapping the names and attribute values between entities in your application and the columns of your database tables.
We will use the Task class to create our method. Task class represents a single operation that does not return a value and that usually executes asynchronously.
public async Task<bool> IsValid(string username, string password)
{
using (IDbConnection db = new SqlConnection(Helper.ConnectionString))
{
if (db.State == ConnectionState.Closed)
db.Open();
var result = await db.ExecuteScalarAsync<int>($"select count(*) from users where password = @password and username = @username", new { password = password, username = username }, commandType: CommandType.Text);
return result != 0;
}
}
The Helper class will read the connection string from the web.config file
class Helper
{
public static string ConnectionString => ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
}
By using the asynchronous .NET 4.5 features. You can dramatically improve data access performance across your entire app or service.