Dapper official documents (1) [Introduction]

What is Dapper

Dapper is a simple .NET object mapper, with the title of " King of Micro ORM " in terms of speed , almost ADO.NETas fast as using the original data reader. ORM is an object-relational mapper, which is responsible for the mapping between the database and the programming language.

DapperIDbConnectionProvide some useful extension methods to query your database through extension .

How does Dapper work

It can be divided into three steps:

  • Create an IDbConnectioninterface object;
  • Write a query SQL to perform CRUD operations;
  • ExecutePass the query SQL as a method parameter.

installation

DapperInstall via NuGet: https://www.nuget.org/packages/Dapper

PM> Install-Package Dapper

Claim

DapperCan work with any database provider because there is no database specific implementation.

method

Dapper will extend your IDbConnectioninterface in the following ways :

  • 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())
{
    
    
        // 执行普通SQL
    var invoices = connection.Query<Invoice>(sqlInvoices).ToList();
    // 执行带参数的SQL
    var invoice = connection.QueryFirstOrDefault(sqlInvoice, new {
    
    InvoiceID = 1});
    // 执行存储过程 
    var affectedRows = connection.Execute(sp, new {
    
     Param1 = "Single_Insert_1" }, commandType: CommandType.StoredProcedure);
}

parameter

The execution and query methods can use parameters in several different ways:

  • Anonymous type
  • Dynamic typing
  • List type
  • String type
// Anonymous
var affectedRows = connection.Execute(sql,
                    new {
    
    Kind = InvoiceKind.WebInvoice, Code = "Single_Insert_1"},
                    commandType: CommandType.StoredProcedure);

// Dynamic
DynamicParameters parameter = new DynamicParameters();

parameter.Add("@Kind", InvoiceKind.WebInvoice, DbType.Int32, ParameterDirection.Input);
parameter.Add("@Code", "Many_Insert_0", DbType.String, ParameterDirection.Input);
parameter.Add("@RowCount", dbType: DbType.Int32, direction: ParameterDirection.ReturnValue);

connection.Execute(sql,
    new {
    
    Kind = InvoiceKind.WebInvoice, Code = "Single_Insert_1"},
    commandType: CommandType.StoredProcedure);

// List
connection.Query<Invoice>(sql, new {
    
    Kind = new[] {
    
    InvoiceKind.StoreInvoice, InvoiceKind.WebInvoice}}).ToList();

// String
connection.Query<Invoice>(sql, new {
    
    Code = new DbString {
    
    Value = "Invoice_1", IsFixedLength = false, Length = 9, IsAnsi = true}}).ToList();

result

The results returned by the query method can be mapped to the following types:

  • Anonymous type
  • Strong type
  • Multiple mapping
  • Multiple results
  • Multiple types
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();
}

tool

  • asynchronous
  • buffer
  • Affairs
  • Stored procedure
// Async
connection.QueryAsync<Invoice>(sql)

// Buffered
connection.Query<Invoice>(sql, buffered: false)

// Transaction
using (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 Procedure
var affectedRows = connection.Execute(sql,
    new {
    
    Kind = InvoiceKind.WebInvoice, Code = "Single_Insert_1"},
    commandType: CommandType.StoredProcedure);

Guess you like

Origin blog.csdn.net/WuLex/article/details/108278536