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. [转]sqlmap使用教程

    sqlmap也是渗透中常用的一个注入工具,其实在注入工具方面,一个sqlmap就足够用了,只要你用的熟,秒杀各种工具,只是一个便捷性问题,sql注入另一方面就是手工党了,这个就另当别论了. 今天把我一 ...

  2. Maximum Size Subarray Sum Equals k LT325

    Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If t ...

  3. 使用vmware安装ubuntu不能上网

    桌面版的话,进入桌面后还可以配置,服务版,我是在安装过程中提示的网络配置时候按照下面的方法手动配置的 安装虚拟机时候要安装网络服务,有的虚拟机在安装过程中可能已经安装好了,主机保持VMware NAT ...

  4. eclipse安装提要

    svn 插件安装http://subclipse.tigris.org/update_1.12.x教程地址http://jingyan.baidu.com/article/f71d60376b4c57 ...

  5. ubuntu禁用n卡驱动(进系统卡死)

    显卡驱动 该发行版依旧内置了Nouveau 开源驱动,这是导致频繁死机的直接原因.接下来要做的三件事情是: 禁用Nouveau 内核模块 安装Intel HD 530 驱动(二选一) 安装NVIDIA ...

  6. Codeforces 1111 简要题解

    文章目录 A题 B题 C题 D题 E题 传送门 A题 传送门 题意简述:把262626个英文字母分成两类A,BA,BA,B,AAA类字符可以转成AAA类字符,BBB类字符可以转成BBB类字符,问给出的 ...

  7. 2018.11.09 洛谷P1110 [ZJOI2007]报表统计(multiset)

    传送门 sb题. 直接用两个multisetmultisetmultiset维护相邻两个数的差值和所有数的前驱后继. 插入一个数的时候更新一下就行了. 代码: #include<bits/std ...

  8. 2018.11.08 NOIP模拟 景点(倍增+矩阵快速幂优化dp)

    传送门 首先按照题意构造出转移矩阵. 然后可以矩阵快速幂求出答案. 但是直接做是O(n3qlogm)O(n^3qlogm)O(n3qlogm)的会TTT掉. 观察要求的东西发现我们只关系一行的答案. ...

  9. hdu-1034(模拟+小朋友分糖)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1034 参考文章:https://blog.csdn.net/zyy173533832/article/ ...

  10. UVa 1610 Party Games(思维)

    题意: 给出一系列字符串,构造出一个最短字符串(可以不在集合中)大于等于其中的一半,小于另一半. 析:首先找出中间的两个字符串,然后暴力找出最短的字符串,满足题意. 代码如下: #include &l ...