1. 程式人生 > >Dapper官方教程翻譯3:Dapper方法之Query

Dapper官方教程翻譯3:Dapper方法之Query

Query方法描述

 

Query方法也是一個擴充套件方法,可以被IDbConnection物件呼叫,可以執行查詢語句,並且對映到結果。

可以對映的型別:

 

Query方法可使用的引數

 

Query方法引數說明
引數名 引數說明
sql 資料庫語句
param 查詢引數
transaction 所使用的事務
buffered 是否快取
commandTimeout 執行超時時間
commandType 語句型別

 

示例:Query執行匿名型別

string sql = "SELECT TOP 10 * FROM OrderDetails";

using (var connection = new SqlCeConnection("Data Source=SqlCe_W3Schools.sdf"))
{	
        //使用var進行型別推斷
	var orderDetail = connection.Query(sql).FirstOrDefault();
        
	FiddleHelper.WriteTable(orderDetail);
}

示例:Query執行強型別查詢

string sql = "SELECT TOP 10 * FROM OrderDetails";

using (var connection = new SqlCeConnection("Data Source=SqlCe_W3Schools.sdf"))
{			
	var orderDetails = connection.Query<OrderDetail>(sql).ToList();

	Console.WriteLine(orderDetails.Count);

	FiddleHelper.WriteTable(orderDetails);
}

示例:執行一對一關係查詢

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();
}

示例:執行一對多關係查詢

string sql = "SELECT TOP 10 * FROM Orders AS A INNER JOIN OrderDetails AS B ON A.OrderID = B.OrderID;";

using (var connection = new SqlCeConnection("Data Source=SqlCe_W3Schools.sdf"))
{			
	var orderDictionary = new Dictionary<int, Order>();


	var list = connection.Query<Order, OrderDetail, Order>(
	sql,
	(order, orderDetail) =>
	{
		Order orderEntry;

		if (!orderDictionary.TryGetValue(order.OrderID, out orderEntry))
		{
		orderEntry = order;
		orderEntry.OrderDetails = new List<OrderDetail>();
		orderDictionary.Add(orderEntry.OrderID, orderEntry);
		}

		orderEntry.OrderDetails.Add(orderDetail);
		return orderEntry;
	},
	splitOn: "OrderID")
	.Distinct()
	.ToList();

	Console.WriteLine(list.Count);

	FiddleHelper.WriteTable(list);
	FiddleHelper.WriteTable(list.First().OrderDetails);
}

示例:Query執行多型別

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);
}