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 SqlCeConnection("Data Source=SqlCe_W3Schools.sdf"))
{
      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 SqlCeConnection("Data Source=SqlCe_W3Schools.sdf"))
{
      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 SqlCeConnection("Data Source=SqlCe_W3Schools.sdf"))
{

      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 SqlCeConnection("Data Source=SqlCe_W3Schools.sdf"))
{   
      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