2、Dapper的使用
1、表结构介绍:
1)课程表

2)成绩表

3)学生表

2、获取数据库连接的工厂类
需要添加System.Configuration和MySql.Data.MySqlClient引用
namespace db
{
/// <summary>
/// 数据库连接工厂
/// </summary>
public class dbFactory
{
public static string connStr = ConfigurationManager.ConnectionStrings["dbConn"].ConnectionString;
private static string prividerName = ConfigurationManager.ConnectionStrings["dbConn"].ProviderName; public static IDbConnection createConn()
{
IDbConnection conn = null;
switch (prividerName)
{
case "System.Data.SqlClient":
conn = new SqlConnection(connStr);
break;
case "MySql.Data.MySqlClient":
conn = new MySqlConnection(connStr);
break;
}
return conn;
}
}
}
3、模型类定义
namespace db.model
{
public class Couser
{
public int id { get; set; }
public string courseName { get; set; }
}
public partial class Score
{
public int id { get; set; }
public int score { get; set; }
public int courseId { get; set; }
public int studentId { get; set; }
}
public partial class Student
{
public int id { get; set; }
public string name { get; set; }
public int sex { get; set; }
public string tel { get; set; }
public string other { get; set; }
}
}
4、需要引入的包
using Dapper;
using DapperExtensions;
using db.model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.SqlClient;
using System.Text;
5、相关使用例子
//查询单个实体
public static Couser query()
{
using (IDbConnection conn = db.dbFactory.createConn())
{
Console.WriteLine(conn.State);
//原生写法
string sql = " SELECT * FROM dbo.Course where id=@id ";
Couser model1 = conn.QueryFirstOrDefault<Couser>(sql, new { id = });
Console.WriteLine(conn.State);
return model1;
}
} //过滤查询like方式1
public static List<Couser> queryWhere1(string courseName)
{
using (IDbConnection conn = db.dbFactory.createConn())
{
//原生写法 模糊查询1
string sql = " SELECT * FROM dbo.Course where charindex(@courseName,courseName)>0 ";
List<Couser> list = conn.Query<Couser>(sql, new { courseName = courseName }).ToList();
return list;
}
} //过滤查询like方式2
public static List<Couser> queryWhere2(string courseName)
{
using (IDbConnection conn = db.dbFactory.createConn())
{
//原生写法 模糊查询2
string sql = " SELECT * FROM dbo.Course where courseName like @courseName ";
List<int> idList = new List<int>();
List<Couser> list = conn.Query<Couser>(sql, new { courseName = $"%{courseName}%" }).ToList();
return list;
}
} //in 查询
public static List<Couser> queryWhere3()
{
List<int> idList = new List<int>();
idList.Add();
idList.Add();
idList.Add();
idList.Add(); using (IDbConnection conn = db.dbFactory.createConn())
{
//原生写法 模糊查询2
string sql = " SELECT * FROM dbo.Course where id in @id ";
List<Couser> list = conn.Query<Couser>(sql, new { id = idList }).ToList();
return list;
}
} //查询所有
public static List<Couser> queryAll()
{
using (IDbConnection conn = db.dbFactory.createConn())
{
//原生写法
string sql = " SELECT * FROM dbo.Course ";
List<Couser> list = conn.Query<Couser>(sql).ToList();
return list;
}
} //返回动态类型
public static List<dynamic> getStudentScore()
{
using (IDbConnection conn = db.dbFactory.createConn())
{
//原生写法
string sql = @" SELECT dbo.Student.name,courseName,score FROM dbo.Course
LEFT JOIN dbo.Score ON dbo.Course.id = coursedId
LEFT JOIN dbo.Student ON studentId = dbo.Student.id; ";
List<dynamic> list = conn.Query<dynamic>(sql).ToList();
return list;
}
} //新增
public static int insert()
{
using (IDbConnection conn = db.dbFactory.createConn())
{
db.model.Couser model = new Couser();
model.courseName = "数据库原理";
//原生写法
string sql = " INSERT INTO dbo.Course(courseName ) VALUES (@courseName) ";
return conn.Execute(sql, model);
}
} //批量新增
public static void insertBatch()
{
using (IDbConnection conn = db.dbFactory.createConn())
{
List<Couser> list = new List<Couser>();
list.Add(new Couser { courseName = "Batch1" });
list.Add(new Couser { courseName = "Batch2" }); Console.WriteLine(conn.State);
//原生写法
string sql = " INSERT INTO dbo.Course(courseName ) VALUES (@courseName) ";
conn.Execute(sql, list);
Console.WriteLine(conn.State); }
} //修改
public static void update()
{
using (IDbConnection conn = dbFactory.createConn())
{
db.model.Couser model = new Couser();
model.id = ;
model.courseName = "数据库原理1";
//原生写法
string sql = " UPDATE dbo.Course SET courseName=@courseName WHERE id=@id ";
conn.Execute(sql, model);
}
} //删除
public static void delete()
{
using (IDbConnection conn = dbFactory.createConn())
{
string sql = " DELETE FROM dbo.Course WHERE id=@id ";
conn.Execute(sql, new { id = });
}
} //事务控制
public static void testTran()
{
using (IDbConnection conn = dbFactory.createConn())
{
conn.Open();
IDbTransaction ts = conn.BeginTransaction();
try
{
string sql1 = " DELETE FROM dbo.Course WHERE id=@id ";
conn.Execute(sql1, new { id = }, ts); string sql2 = " INSERT INTO dbo.Course(id, courseName ) VALUES (N'4', N'sdfsfd') ";
conn.Execute(sql2, new { id = }, ts); ts.Commit();
}
catch (Exception ex)
{
ts.Rollback();
}
finally
{
conn.Close();
}
}
}
2、Dapper的使用的更多相关文章
- Dapper逆天入门~强类型,动态类型,多映射,多返回值,增删改查+存储过程+事物案例演示
Dapper的牛逼就不扯蛋了,答应群友做个入门Demo的,现有园友需要,那么公开分享一下: 完整Demo:http://pan.baidu.com/s/1i3TcEzj 注 意 事 项:http:// ...
- Dapper扩展之~~~Dapper.Contrib
平台之大势何人能挡? 带着你的Net飞奔吧!http://www.cnblogs.com/dunitian/p/4822808.html#skill 上一篇文章:Dapper逆天入门~强类型,动态类型 ...
- 由Dapper QueryMultiple 返回数据的问题得出==》Dapper QueryMultiple并不会帮我们识别多个返回值的顺序
异常汇总:http://www.cnblogs.com/dunitian/p/4523006.html#dapper 今天帮群友整理Dapper基础教程的时候手脚快了点,然后遇到了一个小问题,Dapp ...
- Dapper.Contrib:GetAsync<T> only supports an entity with a [Key] or an [ExplicitKey] property
异常处理:http://www.cnblogs.com/dunitian/p/4523006.html#dapper 原来Model是这样滴 修改后是这样滴 注意点:Model里面的Table和Key ...
- Dapper where Id in的解决方案
简单记一下,一会出去有点事情~ 我们一般写sql都是==>update NoteInfo set NDataStatus=@NDataStatus where NId in (@NIds) Da ...
- ASP.NET Core 1.0 使用 Dapper 操作 MySql(包含事务)
操作 MySql 数据库使用MySql.Data程序包(MySql 开发,其他第三方可能会有些问题). project.json 代码: { "version": "1. ...
- Asp.Net Core + Dapper + Repository 模式 + TDD 学习笔记
0x00 前言 之前一直使用的是 EF ,做了一个简单的小项目后发现 EF 的表现并不是很好,就比如联表查询,因为现在的 EF Core 也没有啥好用的分析工具,所以也不知道该怎么写 Linq 生成出 ...
- 搭建一套自己实用的.net架构(3)续 【ORM Dapper+DapperExtensions+Lambda】
前言 继之前发的帖子[ORM-Dapper+DapperExtensions],对Dapper的扩展代码也进行了改进,同时加入Dapper 对Lambda表达式的支持. 由于之前缺乏对Lambda的知 ...
- mono for android中使用dapper或petapoco对sqlite进行数据操作
在mono for android中使用dapper或petapoco,很简单,新建android 类库项目,直接把原来的文件复制过来,对Connection连接报错部分进行注释和修改就可以运行了.( ...
- Dapper:The member of type SeoTKD cannot be used as a parameter Value
异常汇总:http://www.cnblogs.com/dunitian/p/4523006.html#dapper 上次说了一下Dapper的扩展Dapper.Contrib http://www. ...
随机推荐
- 让Drewtech的J2534 ToolBox 软件支持任何J2534的设备
更改windows注册表中的FunctionLibrary和ConfigApplication,将DLL和exe路径替换原来的,其他不要动. 或者 create second key in regis ...
- python中在计算机视觉中的库及基础用法
基于python脚本语开发的数字图像处理包有很多,常见的比如PIL.Pillow.opencv.scikit-image等.PIL和pillow只提供了基础的数字图像处理,功能有限:OpenCV实际上 ...
- SQL Server新增用户并控制访问权限设置。
新增用户: 一.进入数据库:[安全性]—>[登录名]—>[新建登录名] 二.在常规选项卡中.如图所示,创建登录名.注意设置默认的数据库. 三.在[用户映射]下设置该用户所能访问的数据库.并 ...
- CF1067E Random Forest Rank
CF1067E Random Forest Rank 可以证明: 一个树的邻接矩阵的秩,等于最大匹配数*2(虽然我只能证明下界是最大匹配) 而树的最大匹配可以贪心, 不妨用DP模拟这个过程 f[x][ ...
- AJAX(二)-实现验证码异步验证功能
案例实现效果 用户在前端输入验证码,按键收起触发异步验证,验证验证码的对错 前端代码 checkcode.jsp <%-- Created by IntelliJ IDEA. User: cxs ...
- Vue. 之 Element获取table中选中的行
Vue. 之 Element获取table中选中的行 问题描述: 如下截图,在Table中选择数据后,然后在点击“统计”按钮,获取Table表中选择的行 解决方案: 1. 给“统计”这个按钮添加一个点 ...
- 关于sublime text2的一些问题(为解决)
1. 编写markdown文件后,如何转成pdf ? 2. 执行程序时,如何在控制台输入数据?
- Spark day03
补充算子 transformations mapPartitionWithIndex 类似于mapPartitions,除此之外还会携带分区的索引值. repartition 增加或减少分区.会产生s ...
- Java中的TreeMap及红黑树
TreeMap: http://blog.csdn.net/tobeandnottobe/article/details/7232664 红黑树: http://blog.chinaunix.net/ ...
- Effective Modern C++:04智能指针
裸指针有着诸多缺点:裸指针的声明中看不出它指向的是单个对象还是数组:裸指针的声明中也无法看出使用完它指向的对象后是否需要删除,也就是声明中看不出裸指针是否拥有其指向的对象:即使知道要析构裸指针指向的对 ...