Introduced
Query method is an extension method of IDbConnection, which can be used to execute a query (select) and map the result to a C # entity (Model, Entity) class
The query results can be mapped to the following types:
- Anonymous Anonymous Types
- Strongly Typed Strong type
- Multi-mapping (one to one) multi-mapping one-to-one
- Multi-mapping (one to many) multi-mapping one-to-many
- Multi-type Multi-type
Parameters
The following table shows the different parameters of the Query method
| name |
Description |
| Sql |
The text of the SQL statement to execute |
| Param |
Command's parameters |
| Transaction |
Transaction |
| Buffered |
True to buffer readeing The results of the query (default = True).
The translation does not come ... |
| CommandTimeout |
Command time-out time |
| CommandType |
Command type
|
Example-Anonymous Query
Raw SQL query can be executed using Query method and map the result to a dynamic list.
Executes the SQL statement string directly, and then maps the result into a list of dynamic types
string " SELECT * from Invoice; " ; using (var connection = my.connectionfactory ()) { connection. Open (); var invoices = connection. Query (SQL). ToList (); My.Result.Show (invoices);}
Example-Strongly typed queries (most commonly used)
Executes the SQL statement string directly, and then maps the result into a list of strongly typed types
string " SELECT * from Invoice; " ; using (var connection = my.connectionfactory ()) { connection. Open (); var invoices = connection. query<invoice>(SQL). ToList (); My.Result.Show (invoices);}
Example-multi-mapping query (one to one)
Raw SQL query can be executed using Query method and map the result of a strongly typed list with a one to one relation. (No Too understanding)
Executes the SQL statement string directly, and then maps the result in a one-to-one relationship to a list of strongly typed types
stringsql ="SELECT * from Invoice as A INNER joins Invoicedetail as B on a.invoiceid = B.invoiceid;";using(varConnection =my.connectionfactory ()) {connection. Open (); varInvoices = connection. Query<invoice, Invoicedetail, invoice>(SQL, (Invoice, Invoicedetail)={invoice. Invoicedetail=Invoicedetail; returnInvoice; }, SplitOn:"Invoiceid") . Distinct (). ToList (); My.Result.Show (invoices);}
Example-Query multi-mapping (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.
Executes the SQL statement string directly, and then maps the result in a one-to-many relationship to a list of strongly typed types
stringsql ="SELECT * from Invoice as A INNER joins Invoiceitem as B on a.invoiceid = B.invoiceid;";using(varConnection =my.connectionfactory ()) {connection. Open (); varInvoicedictionary =Newdictionary<int, invoice>(); varInvoices = connection. Query<invoice, Invoiceitem, invoice>(SQL, (Invoice, Invoiceitem)={Invoice invoiceentry; if(!invoicedictionary.trygetvalue (Invoice. Invoiceid, outinvoiceentry)) {Invoiceentry=Invoice; Invoiceentry.items=NewList<invoiceitem>(); Invoicedictionary.add (Invoiceentry.invoiceid, invoiceentry); } invoiceEntry.Items.Add (Invoiceitem); returnInvoiceentry; }, SplitOn:"Invoiceid") . Distinct (). ToList (); My.Result.Show (invoices);}
Example-Query Multi-type
Raw SQL query can be executed using Query method and map the result to a list of different types.
stringsql ="SELECT * from Invoice;";using(varConnection =my.connectionfactory ()) {connection. Open (); varInvoices =NewList<invoice>(); using(varReader =connection. ExecuteReader (SQL)) {varStoreinvoiceparser = reader. Getrowparser<storeinvoice>(); varWebinvoiceparser = reader. Getrowparser<webinvoice>(); while(reader. Read ()) {Invoice Invoice; Switch((invoicekind) reader. GetInt32 (reader. GetOrdinal ("Kind"))) { CaseInvoiceKind.StoreInvoice:invoice=Storeinvoiceparser (reader); Break; CaseInvoiceKind.WebInvoice:invoice=Webinvoiceparser (reader); Break; default: Throw NewException (exceptionmessage.generalexception); } invoices. ADD (Invoice); }} My.Result.Show (invoices);}
Reference article:
Http://dapper-tutorial.net/query
Dapper Getting Started Tutorial (iii)--dapper query