1.安装,直接用nuget搜索Dapper就行,不过只支持框架4.5.1

2.数据库测试表

CREATE TABLE [dbo].[Student](
[ID] [bigint] NULL,
[Name] [nvarchar](50) NULL,
[Birthday] [date] NULL,
[TeacherID] [bigint] NULL,
[Level] [smallint] NULL,
[Remark] [nvarchar](50) NULL
) ON [PRIMARY] GO
CREATE TABLE [dbo].[Teacher](
[ID] [bigint] NULL,
[Name] [nvarchar](50) NULL,
[Birthday] [date] NULL,
[Gender] [bit] NULL
) ON [PRIMARY]

3.Person

   class Person
{
public int ID { get; set; }
public string Name { get; set; }
public DateTime Birthday { get; set; }
}

4.Student

 class Student:Person
{ /// <summary>
/// 这里故意加了加了个一个实体Teacher,在数据库里面,只有一个TeacherID
/// </summary>
public Teacher Teacher{set;get;} public int TeacherID
{
set;
get;
}
public int? Level{set;get;} public string Remark{set;get;} }

5.Teacher

   class Teacher:Person
{
public bool Gender { get; set; }
}

6.测试代码

 class Program
{
static IDbConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["dbstring"].ConnectionString);
static void Main(string[] args)
{
string sql = string.Empty;
{
//普通插入
sql = "insert into Teacher values(@ID,@Name,@Birthday,@Gender) ";
var max = db.Query<int>("select isnull(max(id),0) id from Teacher");
var maxID = max == null ? : max.FirstOrDefault();
Teacher t = new Teacher() { ID = (maxID + ), Gender = false, Name = "Miss Gao", Birthday = DateTime.Now.AddYears(-) };
var result = db.Execute(sql, t);
} {
// //普通插入
var max = db.Query<int>("select isnull(max(id),0) id from student");
var maxID = max == null ? : max.FirstOrDefault();
sql = "insert into student values(@ID,@Name,@Birthday,@TeacherID,@Level,@Remark)";
var result = db.Execute(sql, new Student() { ID = (maxID + ), Name = "Jim", TeacherID = , Remark = "a", Birthday = DateTime.Now, Level = }); }
{//无条件查询
var result1 = db.Query<Student>("Select * from Student");//
var result2 = db.Query<Student>("Select [ID] ,[Name] ,[Birthday] ,[TeacherID] ,[Level] ,[Remark] from Student");//
} {//带条件查询
var result1 = db.Query<Student>("Select * from Student where name=@Name", new Student() { Name = "Jim" });//
var result2 = db.Query<Student>("Select * from Student where id=@ID", new Student() { ID = });//
var result3 = db.Query<Student>("Select [ID] ,[Name] ,[Birthday] ,[TeacherID] ,[Level] ,[Remark] from Student", new { Name = "Jim" });//
} {//联合查询
sql = @"Select *
from Student a join teacher b on a.TeacherID = b.id --where a.ID=@ID";//这里加条件怎么办?拼接SQL?
var result = db.Query<Student, Teacher, Student>(sql, (s, t) => { s.Teacher = t; return s; }); } {//联合查询2
sql = @"Select * from Student where id=@ID ;
select * from teacher where ID = (select teacherid from student where id=@ID)";
var result = db.QueryMultiple(sql, new { ID=});
var s = result.Read<Student>().FirstOrDefault();
s.Teacher = result.Read<Teacher>().FirstOrDefault();
} {//联合查询3
sql = @"Select * from Student where id=@ID ;
select * from teacher where Name =@Name";
var result = db.QueryMultiple(sql, new { ID = ,Name="Miss Gao" });//这里的参数 是很奇怪的哦
var s = result.Read<Student>().FirstOrDefault();
var t= result.Read<Teacher>().FirstOrDefault();
}
}
}

[C#]Dapper学习笔记的更多相关文章

  1. Dapper学习笔记(1)-开始

    Dapper是一款开源的轻量级ORM工具,源代码下载地址为https://github.com/StackExchange/dapper-dot-net,其具有以下特点: 1.Dapper是一个轻型的 ...

  2. Dapper学习笔记(一)

    https://github.com/StackExchange/dapper-dot-net Dapper是对IDbConnection的扩展,需要使用Dapper提供的扩展只需要把SqlMappe ...

  3. Dapper学习笔记(4)-事务

    Dapper中对事务的处理也非常简单,如下代码所示: private void DapperTransaction() { using (IDbConnection con = OpenConnect ...

  4. Dapper学习笔记(2)-链接引用

    在研究Dapper源码时发现Dapper NET45类库中的SqlMapper.cs文件前面有个蓝色的箭头图标,发现在Dapper NET45文件夹下根本不存在SqlMapper.cs文件,其文件属性 ...

  5. Dapper 学习笔记

    一.基础 1.Dapper代码就一个SqlMapper.cs文件, 前人测试Dapper速度较快 的Orm,读取速度接近IDataReader,超过DataTable. 2.a.下载地址 https: ...

  6. Dapper学习笔记

    听说有个轻量化的orm Dapper,我就去了解下.试着对Sql Server和Mysql进行增删改查,体验不错.它不如EF臃肿,也比一般的封装灵活,比如我们封装了一个映射类.利用反射,在Execut ...

  7. Dapper学习笔记(5)-存储过程

    一.无参存储过程 第一步:创建一个不带参数的存储过程,代码如下: CREATE PROCEDURE [dbo].[QueryRoleNoParms] AS BEGIN SELECT * FROM T_ ...

  8. Dapper学习笔记(3)-增、删、改、查

    一.建表 在数据库中建立如下三张表: CREATE TABLE [dbo].[T_User] ( , ) PRIMARY KEY NOT NULL, ) NOT NULL, ) NULL, ) NUL ...

  9. 一起学ASP.NET Core 2.0学习笔记(二): ef core2.0 及mysql provider 、Fluent API相关配置及迁移

    不得不说微软的技术迭代还是很快的,上了微软的船就得跟着她走下去,前文一起学ASP.NET Core 2.0学习笔记(一): CentOS下 .net core2 sdk nginx.superviso ...

随机推荐

  1. Partition Array into Disjoint Intervals LT915

    Given an array A, partition it into two (contiguous) subarrays left and right so that: Every element ...

  2. json数据映射填充到html元素显示

    映射算法做了改进,支持name重复映射 <!DOCTYPE html> <html> <head> <meta charset="UTF-8&quo ...

  3. hehe,网易邮箱已经流氓到这个地步了

    网易邮箱现在感觉作死,申请个邮箱还要下载你的APP,好,你牛逼,再见. 这是态度的问题. 最近丢了5亿的用户信息死不承认,撞库能把密保问题给撞出来? 如果真是撞库的话,丁三石养猪也已经感染口蹄疫了吧.

  4. Twitter 相关APP开发

    首先要获取 Consumer Key (API Key), Consumer Secret (API Secret):最好申请Access Token 和Access Token Secret,不然验 ...

  5. 在 Anaconda下解决国内安装tensorflow等下载慢和中断,出错,异常问题的一点思路

    把镜像地址改为清华大学开源软件镜像站,打开 管理员身份打开cmd 输入conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/ ...

  6. vue组件实现查看大图效果

    使用的index.vue代码 <template> <img :src="imgUrl" @click="clickImg($event)"& ...

  7. 2018.10.26 bzoj2721: [Violet 5]樱花(数论)

    传送门 推一波式子: 1x+1y=1n!\frac 1 x+\frac 1 y=\frac 1 {n!}x1​+y1​=n!1​ =>xy−x∗n!−y∗n!xy-x*n!-y*n!xy−x∗n ...

  8. android检测手机是否安装某个app

    public static boolean isAvilible(Context context, String packageName){ //获取packagemanager final Pack ...

  9. ssh scp 加端口

    scp -P one-infrastructure-api.tar.gz console@172.31.16.2:/root/ ssh -p console@172.31.16.2

  10. telegraph.conf配置

    原文:http://www.cnblogs.com/smail-bao/p/7002361.html 1.操作系统基础监控指标配置标准 基础监控使用通用的全局配置文件telegraf.conf,以下只 ...