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 存储过程

  • 相关阅读:
    ASP.NET生成静态页
    IE9下silverlight 里边MessageBox.Show 失效!
    Android与iOS:谁更适合HTML 5?
    编程修改IIS7的MIME类型的问题
    (译)理解“渐进增强(Progressive Enhancement)”
    C# 获取msi文件的ProductCode
    修改chrome浏览器useragent;在电脑上也能打开pad 或者 iphone 的专用网站
    开始的iOS编程之前的准备
    测试博客
    Google Maps Android API v2
  • 原文地址:https://www.cnblogs.com/handsomer/p/5038857.html
Copyright © 2011-2022 走看看