Translation Dapper Tutorials

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.