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

  • 相关阅读:
    python学习手册 (第3版)
    服务器搭建
    阿里云 大数据 云计算 分布式
    PS插件开发plugin
    GIS九交模型
    人脸识别 人工智能(AI)
    Github上发布托管和下载
    RPLiDAR 激光雷达探测地面高程
    linux内核调试
    convex hull
  • 原文地址:https://www.cnblogs.com/handsomer/p/5038857.html
Copyright © 2011-2022 走看看