码迷,mamicode.com
首页 > 移动开发 > 详细

轻量级ORM框架 第二篇:Dapper

时间:2019-01-03 19:24:38      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:轻量级   框架   轻量   操作   cti   string   orm   sql   执行   

 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);

技术分享图片

 

轻量级ORM框架 第二篇:Dapper

标签:轻量级   框架   轻量   操作   cti   string   orm   sql   执行   

原文地址:https://www.cnblogs.com/BabyRui/p/10215923.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!