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

  • 相关阅读:
    VMware下ubuntu与win8共享文件时/mnt/hgfs目录为空的解决办法
    Flex Array内置排序方法的使用
    Flex 选项卡加载方式简介
    Flash Builder 4.6 基本设置
    Flash Builder 4.6 找不到所需的Adobe Flash Player
    2 python--工具pycharm
    1 python--安装
    安装aix补丁包
    python_day02 上节课知识点回顾
    vue组件局部与全局注册的区别
  • 原文地址:https://www.cnblogs.com/handsomer/p/5038857.html
Copyright © 2011-2022 走看看