Dapper官方教程翻译1:Dapper是什么?(转)

何海
2023-12-01

Dapper官方教程翻译1:Dapper是什么?

什么是Dapper?

Dapper是.NET的一个简单的对象映射器,在速度上拥有微ORM之王的头衔,并且几乎与使用原始ADO.NET数据读取器的速度一样快。ORM是一个对象关系映射器,它负责数据库和编程语言之间的映射。Dapper通过提供有用的扩展方法来查询数据库,从而扩展IDbConnection。

 

如何使用Dapper?

 

使用Dapper只需要三步:

1.创建IDbConnecton对象

2.写一个CURD语句

3.将语句作为参数传递给Execute方法。

 

Dapper安装:

 

使用NuGet命令:Install-Package Dapper

 

数据库要求:

 

只要是提供了数据库Provider的数据库,就可以用Dapper。

 

Dapper扩展方法:

 

 

  1.  
    string sqlOrderDetails = "SELECT TOP 5 * FROM OrderDetails;";
  2.  
    string sqlOrderDetail = "SELECT * FROM OrderDetails WHERE OrderDetailID = @OrderDetailID;";
  3.  
    string sqlCustomerInsert = "INSERT INTO Customers (CustomerName) Values (@CustomerName);";
  4.  
     
  5.  
    using (var connection = new SqlCeConnection("Data Source=SqlCe_W3Schools.sdf"))
  6.  
    {
  7.  
    var orderDetails = connection.Query<OrderDetail>(sqlOrderDetails).ToList();
  8.  
    var orderDetail = connection.QueryFirstOrDefault<OrderDetail>(sqlOrderDetail, new {OrderDetailID = 1});
  9.  
    var affectedRows = connection.Execute(sqlCustomerInsert, new {CustomerName = "Mark"});
  10.  
     
  11.  
    Console.WriteLine(orderDetails.Count);
  12.  
    Console.WriteLine(affectedRows);
  13.  
     
  14.  
    FiddleHelper.WriteTable(orderDetails);
  15.  
    FiddleHelper.WriteTable( new List<OrderDetail>() { orderDetail });
  16.  
    }

 

Dapper参数:

 

  1.  
    // Anonymous
  2.  
    var affectedRows = connection.Execute(sql,
  3.  
    new {Kind = InvoiceKind.WebInvoice, Code = "Single_Insert_1"},
  4.  
    commandType: CommandType.StoredProcedure);
  5.  
     
  6.  
    // Dynamic
  7.  
    DynamicParameters parameter = new DynamicParameters();
  8.  
     
  9.  
    parameter.Add( "@Kind", InvoiceKind.WebInvoice, DbType.Int32, ParameterDirection.Input);
  10.  
    parameter.Add( "@Code", "Many_Insert_0", DbType.String, ParameterDirection.Input);
  11.  
    parameter.Add( "@RowCount", dbType: DbType.Int32, direction: ParameterDirection.ReturnValue);
  12.  
     
  13.  
    connection.Execute(sql,
  14.  
    parameter,
  15.  
    commandType: CommandType.StoredProcedure);
  16.  
     
  17.  
    int rowCount = parameter.Get<int>("@RowCount");
  18.  
     
  19.  
    // List
  20.  
    connection.Query<Invoice>(sql, new {Kind = new[] {InvoiceKind.StoreInvoice, InvoiceKind.WebInvoice}}).ToList();
  21.  
    // String
  22.  
    connection.Query<Invoice>(sql, new {Code = new DbString {Value = "Invoice_1", IsFixedLength = false, Length = 9, IsAnsi = true}}).ToList();

 

Dapper执行结果

 

  1.  
    string sqlOrderDetails = "SELECT TOP 10 * FROM OrderDetails;";
  2.  
     
  3.  
    using (var connection = new SqlCeConnection("Data Source=SqlCe_W3Schools.sdf"))
  4.  
    {
  5.  
    var anonymousList = connection.Query(sqlOrderDetails).ToList();
  6.  
    var orderDetails = connection.Query<OrderDetail>(sqlOrderDetails).ToList();
  7.  
     
  8.  
    Console.WriteLine(anonymousList.Count);
  9.  
    Console.WriteLine(orderDetails.Count);
  10.  
     
  11.  
    FiddleHelper.WriteTable(orderDetails);
  12.  
     
  13.  
    FiddleHelper.WriteTable(connection.Query(sqlOrderDetails).FirstOrDefault());
  14.  
    }

 

Dapper其他辅助工具

 

  1.  
    // Async
  2.  
    connection.QueryAsync<Invoice>(sql)
  3.  
     
  4.  
    // Buffered
  5.  
    connection.Query<Invoice>(sql, buffered: false)
  6.  
     
  7.  
    // Transaction
  8.  
    using (var transaction = connection.BeginTransaction())
  9.  
    {
  10.  
    var affectedRows = connection.Execute(sql,
  11.  
    new {Kind = InvoiceKind.WebInvoice, Code = "Single_Insert_1"},
  12.  
    commandType: CommandType.StoredProcedure,
  13.  
    transaction: transaction);
  14.  
     
  15.  
    transaction.Commit();
  16.  
    }
  17.  
     
  18.  
    // Stored Procedure
  19.  
    var affectedRows = connection.Execute(sql,
  20.  
    new {Kind = InvoiceKind.WebInvoice, Code = "Single_Insert_1"},
  21.  
    commandType: CommandType.StoredProcedure);

 

转载于:https://www.cnblogs.com/LiZhongZhongY/p/10991638.html

 类似资料: