Dapper ORM 一個NuGet庫,它將擴展您的IDbConnection接口
有很棒的擴展!

string sql = "SELECT * FROM Invoices";

using (var conn = My.ConnectionFactory())
{
    var invoices = conn.Query<Invoice>(sql);
}

Dapper FAQ



Dapper是ORM嗎?

是的,不!人們仍然在爭論它。 Dapper贏得了C#Micro ORM之王的稱號,但被多人視為.NET的簡單對象映射器。



Dapper比實體框架更好嗎?

是的,不!當人們想要以最佳性能自己編寫SQL查詢時,他們會更喜歡Dapper。



Dapper SQL注入安全嗎?

是的,如果您使用參數化查詢,那麼它是100%安全的!



Dapper是否支持批量插入?

不,但第三方庫確實如此: Dapper Plus 。它是一個主要的庫,可以擴展Dapper的所有批量操作。

Learn More


Dapper是否支持我的數據庫提供程序?

可能是的,因為Dapper提供了IDbConnection接口的擴展。編寫與數據庫提供程序兼容的SQL是你的工作。



Dapper支持交易嗎?

是的,Dapper支持事務和每個可以使用的方法都有一個可選參數來指定它。


Dapper Features

Dapper - Execute

Execute a command one or multiple times and return the number of affected rows.

  • Stored Procedure
  • INSERT statement
  • UPDATE statement
  • DELETE statement
Dapper - Execute Example
string sql = "INSERT INTO Customers (CustomerName) Values (@CustomerName);";

using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
{
      var affectedRows = connection.Execute(sql, new {CustomerName = "Mark"});

      Console.WriteLine(affectedRows);
   
      var customer = connection.Query<Customer>("Select * FROM CUSTOMERS WHERE CustomerName = 'Mark'").ToList();
   
      FiddleHelper.WriteTable(customer);
}

Try it

Dapper - Query

Execute a query and map the result.

Dapper - Query Example
string sql = "SELECT TOP 10 * FROM OrderDetails";

using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
{
      var orderDetails = connection.Query<OrderDetail>(sql).ToList();
      
      FiddleHelper.WriteTable(orderDetails);
}

Try it

Dapper - Parameters

Use parameter in your Dapper query.

Dapper - Parameter Example
string sql = "INSERT INTO Customers (CustomerName) Values (@CustomerName);";

using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
{

      var affectedRows = connection.Execute(sql, new {CustomerName = "Mark"});

      Console.WriteLine(affectedRows);
   
      var customer = connection.Query<Customer>("Select * FROM CUSTOMERS WHERE CustomerName = 'Mark'").ToList();
      
      FiddleHelper.WriteTable(customer);
}

Try it

Dapper - Result

Map the query result to different types.

Dapper - Result Example
string sql = "SELECT TOP 10 * FROM OrderDetails";

using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
{   
      var orderDetails = connection.Query<OrderDetail>(sql).ToList();

      Console.WriteLine(orderDetails.Count);
      
      FiddleHelper.WriteTable(orderDetails);
}

Try it

Consulting

Your company requires some custom solution to extend Dapper with more features?

Contact us to learn about our consultation services:
info@zzzprojects.com

You can also find some answers/ help:

Stack Overflow Issue tracker