[譯]Dapper教程

來源:互聯網
上載者:User

標籤:ram   exchange   執行   code   安裝   var   syn   資料庫   http   

 

原文:Dapper Tutorial ,擷取更好瀏覽體驗請跳轉到GitBook[點擊訪問]。

什麼是Dapper

Dapper是一個簡單的.NET對象映射器,在速度方面具有"King of Micro ORM"的頭銜,幾乎與使用原始的ADO.NET資料讀取器一樣快。ORM是一個對象關係映射器,它負責資料庫和程式設計語言之間的映射。

Dapper通過擴充IDbConnection提供一些有用的擴充方法去查詢您的資料庫。

Dapper是如何工作的

它可以分為三個步驟:

  • 建立一個IDbConnection介面對象;
  • 編寫一個查詢SQL來執行CRUD操作;
  • 將查詢SQL作為Execute方法的參數傳遞。
安裝

Dapper通過NuGet安裝:https://www.nuget.org/packages/Dapper

PM> Install-Package Dapper
要求

Dapper可以與任何資料庫提供者一起工作,因為沒有資料庫特定的實現。

方法

Dapper會用以下幾個方法擴充您的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()){        // 執行普通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);}
參數

執行和查詢方法可以用以下幾種不同的方式使用參數:

  • 匿名型別
  • 動態類型
  • 清單類型
  • 字串類型
// Anonymousvar affectedRows = connection.Execute(sql,                    new {Kind = InvoiceKind.WebInvoice, Code = "Single_Insert_1"},                    commandType: CommandType.StoredProcedure);// DynamicDynamicParameters 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);// 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();
結果

查詢方法返回的結果可以映射到以下幾種類型:

  • 匿名型別
  • 強型別
  • 多映射
  • 多結果
  • 多類型
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();}
工具
  • 非同步
  • 緩衝
  • 事務
  • 預存程序
// 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

出處:http://www.cnblogs.com/esofar/p/dapper.html

本文著作權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連結。

 分類: .NET技術

[譯]Dapper教程

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.