zoukankan      html  css  js  c++  java
  • 轻量级ORM框架 第二篇:Dapper

     1. in操作

    IDbConnection connection = new SqlConnection("Data Source=.;Initial Catalog=DapperTest;Integrated Security=True;MultipleActiveResultSets=True");
                var sql = "select * from Users where Email in @emails";
                var info = connection.Query<Users>(sql, new { emails = new string[2] { "1QQ", "7QQ" } });

    2. 多条sql执行

                IDbConnection connection = new SqlConnection("Data Source=.;Initial Catalog=DapperTest;Integrated Security=True;MultipleActiveResultSets=True");  
                var sql = "select * from Product; select * from Users";
                var multiReader = connection.QueryMultiple(sql);
                var productList = multiReader.Read<Product>();
                var userList = multiReader.Read<Users>();
                multiReader.Dispose();
    

    3.多表join操作

           var sql = @"select  p.ProductName,p.CreateTime,u.UserName
                            from Product as p
                            join Users as u
                            on p.UserID = u.UserID
                            where p.CreateTime > '2019-1-1'; ";
    
                var result = connection.Query<Product, Users,Product>(sql,
                                        (product, users) =>
                                        {
                                            product.UsersOwner = users;
                                            return product;
                                        },splitOn:"UserName");
    

      

    4.支持存储过程

    use DapperTest
    set ansI_nulls on
    go
    set quoted_identifier on
    go
    create proc [dbo].[sp_GetUsers] 
    @id int  
    as 
    begin  
    select * from Users where UserID = @id ;  
    end
    

     

    var info = connection.Query<Users>("sp_GetUsers", new { id = 10 },
                                           commandType: CommandType.StoredProcedure);

  • 相关阅读:
    获取与端点的连接
    判断div内滚动条是否在底部
    MVC的使用!
    格式转换解决存取数据安全问题
    JQuery input file 上传图片
    contenteditable 常用的一些CSS !!
    C# 生成Json类型数据
    生成Excel
    生成验证码
    图片水印
  • 原文地址:https://www.cnblogs.com/BabyRui/p/10215923.html
Copyright © 2011-2022 走看看