當前位置:
首頁 > 知識 > Dapper入門教程(三)——Dapper Query查詢

Dapper入門教程(三)——Dapper Query查詢

介紹

查詢方法(Query)是IDbConnection的擴展方法,它可以用來執行查詢(select)並映射結果到C#實體(Model、Entity)類

查詢結果可以映射成如下類型:

  • Anonymous 匿名類型
  • Strongly Typed 強類型
  • Multi-Mapping (One to One) 多映射 一對一
  • Multi-Mapping (One to Many) 多映射 一對多
  • Multi-Type 多類型

參數

下面表格中顯示了Query方法的不同參數


名稱 描述
sql 要執行的sql語句文本
param command的參數
transaction 事務
buffered True to buffer readeing the results of the query (default = true).翻譯不來。。。
commandTimeout command超時時間
commandType command類型

示例 - 匿名查詢

Raw SQL query can be executed using Query method and map the result to a dynamic list.

直接執行SQL語句字元串,然後將結果映射成 dynamic類型的List中

string sql = "SELECT * FROM Invoice;";

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

var invoices = connection.Query(sql).ToList;

My.Result.Show(invoices);
}

示例 - 強類型查詢(最常用)

直接執行SQL語句字元串,然後將結果映射成強類型類型的List中

string sql = "SELECT * FROM Invoice;";

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

var invoices = connection.Query<Invoice>(sql).ToList;

My.Result.Show(invoices);
}

Dapper入門教程(三)——Dapper Query查詢

示例 - 多映射查詢 (One to One)

Raw SQL query can be executed using Query method and map the result to a strongly typed list with a one to one relation.(沒太理解)

直接執行SQL語句字元串,然後將結果用一對一的關係映射成強類型類型的List中

string sql = "SELECT * FROM Invoice AS A INNER JOIN InvoiceDetail AS B ON A.InvoiceID = B.InvoiceID;";

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

var invoices = connection.Query<Invoice, InvoiceDetail, Invoice>(
sql,
(invoice, invoiceDetail) =>
{
invoice.InvoiceDetail = invoiceDetail;
return invoice;
},
splitOn: "InvoiceID")
.Distinct
.ToList;

My.Result.Show(invoices);
}

Dapper入門教程(三)——Dapper Query查詢

示例 - 查詢多映射 (One to Many)

Raw SQL query can be executed using Query method and map the result to a strongly typed list with a one to many relations.

直接執行SQL語句字元串,然後將結果用一對多的關係映射成強類型類型的List中

string sql = "SELECT * FROM Invoice AS A INNER JOIN InvoiceItem AS B ON A.InvoiceID = B.InvoiceID;";

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

var invoiceDictionary = new Dictionary<int, Invoice>;

var invoices = connection.Query<Invoice, InvoiceItem, Invoice>(
sql,
(invoice, invoiceItem) =>
{
Invoice invoiceEntry;

if (!invoiceDictionary.TryGetValue(invoice.InvoiceID, out invoiceEntry))
{
invoiceEntry = invoice;
invoiceEntry.Items = new List<InvoiceItem>;
invoiceDictionary.Add(invoiceEntry.InvoiceID, invoiceEntry);
}

invoiceEntry.Items.Add(invoiceItem);
return invoiceEntry;
},
splitOn: "InvoiceID")
.Distinct
.ToList;

My.Result.Show(invoices);
}

Dapper入門教程(三)——Dapper Query查詢

示例 - Query Multi-Type

Raw SQL query can be executed using Query method and map the result to a list of different types.

string sql = "SELECT * FROM Invoice;";

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

var invoices = new List<Invoice>;

using (var reader = connection.ExecuteReader(sql))
{
var storeInvoiceParser = reader.GetRowParser<StoreInvoice>;
var webInvoiceParser = reader.GetRowParser<WebInvoice>;

while (reader.Read)
{
Invoice invoice;

switch ((InvoiceKind) reader.GetInt32(reader.GetOrdinal("Kind")))
{
case InvoiceKind.StoreInvoice:
invoice = storeInvoiceParser(reader);
break;
case InvoiceKind.WebInvoice:
invoice = webInvoiceParser(reader);
break;
default:
throw new Exception(ExceptionMessage.GeneralException);
}

invoices.Add(invoice);
}
}

My.Result.Show(invoices);
}

Dapper入門教程(三)——Dapper Query查詢

喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 達人科技 的精彩文章:

Restful介面調用方法超詳細總結
WordPress解析系列之PHP編寫hook鉤子原理簡單實例
vue+mockjs 模擬數據,實現前後端分離開發
Handler實現線程之間的通信-下載文件動態更新進度條

TAG:達人科技 |

您可能感興趣

Docker教程:Docker Compose入門
Web Scraper 入門教程
Apache Commons IO 入門教程
Python 繪圖庫 Matplotlib 入門教程
【進階篇】Recurrent Group教程
不打開文件獲取名稱。Excel VBA Application對象GetOpenFilename方法教程
Snapseed教程
Adobe illustrator 2018Mac版安裝教程
下載 | 最新教程《Artifical Neural Networks》
Adobe Illustrator CS6安裝破解教程
AMD銳龍Threadripper處理器怎麼安裝?銳龍Threadripper安裝教程
Excel文本函數search和searchb教程
TensorFlow Probability 概率編程入門級實操教程
Spring Boot 基礎教程 ( 二 ) :快速構建 Spring Boot/Cloud 工程
Mac效率神器Alfred系列教程-iTunes Mini Player
Apache Shiro 10分鐘入門教程
XamarinEssentials教程獲取首選項的值
雷神911最新的6代機重裝系統提示no bootable device.systemstop修改bios教程
photoshop教程|UI教程|cake圖標
Shopify乾貨!Shopify店鋪主題設置&Header配置教程