猿教程 Logo

Dapper-异步

Dapper还使用Async(异步)方法扩展了IDbConnection接口:

  • ExecuteAsync

  • QueryAsync

  • QueryFirstAsync

  • QueryFirstOrDefaultAsync

  • QuerySingleAsync

  • QuerySingleOrDefaultAsync

  • QueryMultipleAsync

我们在本教程中只添加非异步版本,以便于阅读。

ExecuteAsync

var sql = "Invoice_Insert";
using (var connection = My.ConnectionFactory())
{
connection.Open();
var affectedRows = connection.ExecuteAsync(sql,
new {Kind = InvoiceKind.WebInvoice, Code = "Single_Insert_1"},
commandType: CommandType.StoredProcedure)
.Result;
}

QueryAsync

var sql = "Invoice_Insert";
using (var connection = My.ConnectionFactory())
{
connection.Open();
var invoices = connection.QueryAsync<Invoice>(sql).Result.ToList();
}

QueryFirstAsync

var sql = "SELECT * FROM Invoice WHERE InvoiceID = @InvoiceID;";
using (var connection = My.ConnectionFactory())
{
connection.Open();
var invoice = connection.QueryFirstAsync
}

QueryFirstOrDefaultAsync

var sql = "SELECT * FROM Invoice WHERE InvoiceID = @InvoiceID;";
using (var connection = My.ConnectionFactory())
{
connection.Open();
var invoice = connection.QueryFirstOrDefaultAsync
}

QuerySingleAsync

var sql = "SELECT * FROM Invoice WHERE InvoiceID = @InvoiceID;";
using (var connection = My.ConnectionFactory())
{
connection.Open();
var invoice = connection.QuerySingleAsync
}

QuerySingleOrDefaultAsync

var sql = "SELECT * FROM Invoice WHERE InvoiceID = @InvoiceID;";
using (var connection = My.ConnectionFactory())
{
connection.Open();
var invoice = connection.QuerySingleOrDefaultAsync
}

QueryMultipleAsync

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
var invoiceItems = multi.Read
}
}



版权声明:本站所有教程均为本站原创或翻译,转载请注明出处,请尊重他人劳动果实。请记住本站地址:www.yuanjiaocheng.net (猿教程) 作者:卿文刚
本文标题: C#环境
本文地址:http://www.yuanjiaocheng.net/Dapper/result-async.html