zoukankan      html  css  js  c++  java
  • Dapper

    本文内容摘自dapper官网
    动态类型
    public static IEnumerable<dynamic> Query (this IDbConnection cnn, string sql, object param = null, SqlTransaction transaction = null, bool buffered = true)

    This method will execute SQL and return a dynamic list.

    Example usage:

    var rows = connection.Query("select 1 A, 2 B union all select 3, 4");
    
    ((int)rows[0].A)
       .IsEqualTo(1);
    
    ((int)rows[0].B)
       .IsEqualTo(2);
    
    ((int)rows[1].A)
       .IsEqualTo(3);
    
    ((int)rows[1].B)
        .IsEqualTo(4);
    多次执行语句

    Execute a Command multiple times

    The same signature also allows you to conveniently and efficiently execute a command multiple times (for example to bulk-load data)

    Example usage:

    connection.Execute(@"insert MyTable(colA, colB) values (@a, @b)",
        new[] { new { a=1, b=1 }, new { a=2, b=2 }, new { a=3, b=3 } }
      ).IsEqualTo(3); // 3 rows inserted: "1,1", "2,2" and "3,3"

    This works for any parameter that implements IEnumerable for some T.

     集合 支持

    List Support

    Dapper allow you to pass in IEnumerable and will automatically parameterize your query.

    For example:

    connection.Query<int>("select * from (select 1 as Id union all select 2 union all select 3) as X where Id in @Ids", new { Ids = new int[] { 1, 2, 3 });

    Will be translated to:

    select * from (select 1 as Id union all select 2 union all select 3) as X where Id in (@Ids1, @Ids2, @Ids3)" // @Ids1 = 1 , @Ids2 = 2 , @Ids2 = 3
    一次执行多条语句

    Multiple Results

    Dapper allows you to process multiple result grids in a single query.

    Example:

    var sql = 
    @"
    select * from Customers where CustomerId = @id
    select * from Orders where CustomerId = @id
    select * from Returns where CustomerId = @id";
    
    using (var multi = connection.QueryMultiple(sql, new {id=selectedId}))
    {
       var customer = multi.Read<Customer>().Single();
       var orders = multi.Read<Order>().ToList();
       var returns = multi.Read<Return>().ToList();
       ...
    } 

    Stored Procedures 存储过程

  • 相关阅读:
    FSBQPIDMI总线的区别
    为什么PCI-e比SATA快这么多?
    chage命令管理用户口令时效
    账户管理groupadd groupmod groupdel usermod usermod userdel
    linux 里 /etc/passwd 、/etc/shadow和/etc/group 文件内容解释
    RPM常见用法
    智力逻辑题
    Android ROM 制作教程
    智能家居项目(2):项目project框架的搭建
    5999卖999!是噱头还是颠覆
  • 原文地址:https://www.cnblogs.com/handsomer/p/5038857.html
Copyright © 2011-2022 走看看