Original: Dapper Tutorial, get a better browsing experience please go to gitbook[click to visit].
What is dapper
Dapper is a simple. NET object mapper with the title "King of Micro ORM" in terms of speed, almost as fast as using the original ADO data reader. An ORM is an object-relational mapper that is responsible for mapping between databases and programming languages.
Dapper IDbConnection provides some useful extension methods to query your database by extension.
How the dapper Works
It can be divided into three steps:
- Create an
IDbConnection interface object;
- Write a query SQL to perform CRUD operations;
- The query SQL is
Execute passed as a parameter to the method.
Installation
Dapper through NuGet installation: Https://www.nuget.org/packages/Dapper
Install-Package Dapper
Requirements
Dapper can work with any database provider because there is no database-specific implementation.
Method
Dapper will extend your interface in the following ways IDbConnection :
- Execute
- Query
- Queryfirst
- Queryfirstordefault
- Querysingle
- Querysingleordefault
- Querymultiple
String sqlinvoices ="SELECT * from Invoice;";String Sqlinvoice ="SELECT * from Invoice WHERE invoiceid = @InvoiceID;"; string sp = "EXEC invoice_insert"; using (var connection = my.connectionfactory ()) { //execute normal SQL var invoices = connection. Query<invoice> (sqlinvoices). ToList (); //Execute SQL var invoice = connection with parameters. Queryfirstordefault (Sqlinvoice, new {Invoiceid = 1}); //Execute stored procedure var affectedrows = connection. Execute (sp, new {Param1 = "Single_insert_1"}, CommandType:CommandType.StoredProcedure);}
Parameters
The execution and query methods can use the parameters in several different ways:
- Anonymous type
- Dynamic type
- List type
- String type
Anonymousvar affectedrows = connection. Execute (SQL,New {Kind = Invoicekind.webinvoice, Code ="Single_insert_1"},CommandType:CommandType.StoredProcedure);Dynamicdynamicparameters parameter =New Dynamicparameters ();p arameter. ADD ("@Kind", Invoicekind.webinvoice, Dbtype.int32, ParameterDirection.Input);p Arameter. ADD ("@Code","Many_insert_0", Dbtype.string, ParameterDirection.Input);p Arameter. ADD ("@RowCount",DbType:DbType.Int32, direction:ParameterDirection.ReturnValue); connection. Execute (SQL, new {Kind = Invoicekind.webinvoice, Code = "Single_insert_1"}, CommandType: CommandType.StoredProcedure); //listconnection.query<invoice> (SQL, new {Kind = new[] {invoicekind.storeinvoice, Invoicekind.webinvoice}}). ToList (); //stringconnection.query<invoice> (SQL, new {Code = new Dbstring {Value = "Invoice_1", Isfixedlength = false, Length = 9, Isansi = True}}). ToList ();
Results
The results returned by the query method can be mapped to the following types:
- Anonymous type
- Strongly typed
- Multi-mapping
- Multiple results
- Multi-type
string sql = "SELECT * FROM Invoice;";using (var connection = My.ConnectionFactory()){ connection.Open(); var anonymousList = connection.Query(sql).ToList(); var invoices = connection.Query<Invoice>(sql).ToList();}
Tools
- Asynchronous
- Buffer
- Transaction
- Stored Procedures
//asyncconnection.queryasync<invoice> (SQL) // bufferedconnection.query<invoice> (SQL, buffered: false) //transactionusing (var Transaction = connection. BeginTransaction ()) {var affectedrows = connection. Execute (SQL, new {Kind = invoicekind.webinvoice, Code = Single_ Insert_1 "}, CommandType:CommandType.StoredProcedure, transaction:transaction); Transaction.commit ();} //Stored procedurevar affectedrows = connection. Execute (SQL, new {Kind = invoicekind.webinvoice, Code = Single_ Insert_1 "}, CommandType:CommandType.StoredProcedure);
Esofar
Source: http://www.cnblogs.com/esofar/p/dapper.html
The copyright of this article is owned by the author and the blog Park, welcome reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to give the original link.
Classification:. NET Technology
Translation Dapper Tutorials