C#中Dapper的使用教程

一、什么是Dapper Dapper是一款轻量级ORM工具(Github)。如果你在小的项目中,使用Entity Framework、NHibernate 来处理大数据访问及关系映射,未免有点杀鸡用牛刀。你又觉得ORM省时省力,这时Dapper 将是你不二的选择。
二、Dapper的优点

  1. 轻量。只有一个文件SqlMapper.cs,编译后就40K的一个很小的Dll.
  2. 速度快。Dapper的速度接近与IDataReader,取列表的数据超过了DataTable。
  3. 支持多种数据库。Dapper可以在所有Ado.net Providers下工作,包括sqlite, sqlce, firebird, oracle, MySQL, PostgreSQL and SQL Server
  4. 可以映射一对一,一对多,多对多等多种关系。
  5. 性能高。通过Emit反射IDataReader的序列队列,来快速的得到和产生对象,性能不错。
  6. 支持FrameWork2.0,3.0,3.5,4.0,4.5
  7. Dapper语法十分简单。并且无须迁就数据库的设计。
三、使用方法 1、添加NuGet程序包Dapper
C#中Dapper的使用教程
文章图片

2、配置文件添加连接数据库的字符串

3、添加DapperTools.cs(添加、批量添加、删除、批量删除、修改、批量修改、查询、查询in的操作、多语句操作)
// // Copyright (c) PlaceholderCompany. All rights reserved.// namespace CSharpStudy{using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Text; using System.Threading.Tasks; using Dapper; internal class DapperTools{public static string ConnectionString = ConfigurationManager.ConnectionStrings["MyStrConn"].ConnectionString; /// /// 添加./// /// 实体类型./// 传入sql执行语句./// 传入实体类型./// int.public static int Add(string sql, T t)where T : class{using (IDbConnection connection = new SqlConnection(ConnectionString)){return connection.Execute(sql, t); }}/// /// 批量添加./// /// 实体类型./// 传入sql执行语句./// 传入泛型类./// int.public static int Add(string sql, List t)where T : class{using (IDbConnection connection = new SqlConnection(ConnectionString)){return connection.Execute(sql, t); }}/// /// 删除./// /// 实体类型./// 传入sql执行语句./// 传入实体类型./// int.public static int Delete(string sql, T t)where T : class{using (IDbConnection connection = new SqlConnection(ConnectionString)){return connection.Execute(sql, t); }}/// /// 批量删除./// /// 实体类型./// 传入sql执行语句./// 传入泛型类./// int.public static int Delete(string sql, List t)where T : class{using (IDbConnection connection = new SqlConnection(ConnectionString)){return connection.Execute(sql, t); }}/// /// 修改./// /// 实体类型./// 传入sql执行语句./// 传入实体类型./// int.public static int Update(string sql, T t)where T : class{using (IDbConnection connection = new SqlConnection(ConnectionString)){return connection.Execute(sql, t); }}/// /// 批量修改./// /// 实体类型./// 传入sql执行语句./// 传入泛型类./// int.public static int Update(string sql, List t)where T : class{using (IDbConnection connection = new SqlConnection(ConnectionString)){return connection.Execute(sql, t); }}/// /// 查询./// /// 实体类型./// 传入sql执行语句./// 泛型类.public static List Query(string sql)where T : class{using (IDbConnection connection = new SqlConnection(ConnectionString)){return connection.Query(sql).ToList(); }}/// /// 查询指定数据./// /// 实体类型./// 传入sql执行语句./// 传入泛型类./// 类.public static T Query(string sql, T t)where T : class{using (IDbConnection connection = new SqlConnection(ConnectionString)){return connection.Query(sql, t).SingleOrDefault(); }}/// /// 查询的in操作./// /// 实体类型./// 传入sql执行语句./// 泛型类.public static List Query(string sql, int[] ids)where T : class{using (IDbConnection connection = new SqlConnection(ConnectionString)){return connection.Query(sql, new { ids }).ToList(); }}/// /// 多语句操作./// /// 实体类型./// 传入sql执行语句.public static void QueryMultiple(string sql){using (IDbConnection connection = new SqlConnection(ConnectionString)){var multiReader = connection.QueryMultiple(sql); var userInfo = multiReader.Read(); var student = multiReader.Read(); multiReader.Dispose(); }}}}

4、添加测试类Student.cs、UserInfo。表数据自己添加
public class Student{public int Id { get; set; }public string Name { get; set; }public int Age { get; set; }public string Memo { get; set; }public DateTime CreateTime { get; set; }public DateTime UpdateTime { get; set; }}

public class UserInfo{public int Id { get; set; }public string Name { get; set; }public int Age { get; set; }public string Memo { get; set; }public DateTime CreateTime { get; set; }public DateTime UpdateTime { get; set; }}

5、调用方法
//-----------------------------------------------------------------------// // Copyright (c) PlaceholderCompany. All rights reserved.// // John Doe//-----------------------------------------------------------------------namespace CSharpStudy{using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Dapper; internal class Program{private static void Main(string[] args){// --------------添加--------------// UserInfo user = new UserInfo(); // user.Name = "九九"; // user.Age = 10; // user.Memo = "这是备注"; // user.CreateTime = DateTime.Now; // string sql = "Insert into UserInfo(Name,Age,Memo,CreateTime) values (@name, @Age, @Memo,@CreateTime)"; // int result = DapperTools.Add(sql, user); // if (result > 0)// {//Console.WriteLine("添加成功"); //Console.ReadKey(); // }// --------------批量添加--------------// UserInfo user = new UserInfo(); // user.Name = "李奎"; // user.Age = 50; // user.Memo = "这是备注"; // user.CreateTime = DateTime.Now; // UserInfo user2 = new UserInfo(); // user2.Name = "梁山伯"; // user2.Age = 54; // user2.Memo = "这是备注"; // user2.CreateTime = DateTime.Now; // List list = new List(); // list.Add(user); // list.Add(user2); // string sql = "Insert into UserInfo(Name,Age,Memo,CreateTime) values (@name, @Age, @Memo,@CreateTime)"; // int result = DapperTools.Add(sql, list); // if (result > 0)// {//Console.WriteLine("添加成功"); //Console.ReadKey(); // }// --------------删除--------------// UserInfo user = new UserInfo(); // user.Id = 18; // string sql = "delete from UserInfo where Id=@Id"; // int result = DapperTools.Delete(sql, user); // if (result > 0)// {//Console.WriteLine("删除成功"); //Console.ReadKey(); // }// --------------批量删除--------------// UserInfo user = new UserInfo(); // user.Id = 15; // UserInfo user2 = new UserInfo(); // user2.Id = 16; // List list = new List(); // list.Add(user); // list.Add(user2); // string sql = "delete from UserInfo where Id=@Id"; // int result = DapperTools.Delete(sql, list); // if (result > 0)// {//Console.WriteLine("添加成功"); //Console.ReadKey(); // }// --------------修改--------------// UserInfo user = new UserInfo(); // user.Id = 14; // user.Name = "九九"; // string sql = "update UserInfo set Name=@Name,UpdateTime=GETDATE() where Id=@ID"; // int result = DapperTools.Update(sql, user); // if (result > 0)// {//Console.WriteLine("修改成功"); //Console.ReadKey(); // }// --------------批量修改--------------// UserInfo user = new UserInfo(); // user.Id = 13; // user.Name = "王文"; // UserInfo user2 = new UserInfo(); // user2.Id = 14; // user2.Name = "冰冰"; // List list = new List(); // list.Add(user); // list.Add(user2); // string sql = "update UserInfo set Name=@Name,UpdateTime=GETDATE() where Id=@ID"; // int result = DapperTools.Update(sql, list); // if (result > 0)// {//Console.WriteLine("修改成功"); //Console.ReadKey(); // }// --------------查询--------------// string sql = "select * from UserInfo"; // List list = DapperTools.Query(sql); // foreach (var item in list)// {//Console.WriteLine(item.Id + "-" + item.Name + "-" + item.Age + "-" + item.Memo); // }// Console.ReadKey(); // --------------查询指定数据--------------// UserInfo user = new UserInfo(); // user.Id = 14; // string sql = "select * from UserInfo where Id=@Id"; // UserInfo userInfo = DapperTools.Query(sql, user); // Console.WriteLine(userInfo.Id + "-" + userInfo.Name + "-" + userInfo.Age + "-" + userInfo.Memo); // Console.ReadKey(); // --------------查询的in操作--------------// string sql = "select * from UserInfo where Id in @ids"; // int[] ids = { 1, 2 }; // List list = DapperTools.Query(sql, ids); // foreach (var item in list)// {//Console.WriteLine(item.Id + "-" + item.Name + "-" + item.Age + "-" + item.Memo); // }// --------------多语句操作--------------// string sql = "select * from userinfo; select * from student"; // DapperTools.QueryMultiple(sql); }}}

源代码:
链接: https://pan.baidu.com/s/1IM3diCqJCoSpvr11H6zddQ
提取码: 6ag9
后续会陆续更新其他资料,喜欢请关注哦!
我的博客:https://www.cnblogs.com/duhaoran
【C#中Dapper的使用教程】到此这篇关于C# Dapper的使用的文章就介绍到这了,更多相关C#使用Dapper内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    推荐阅读