运维开发网

轻量级ORM框架Dapper应用程序

运维开发网 https://www.qedev.com 2022-08-13 19:42 出处:网络
这篇文章介绍了使用Dapper实现CURD操作的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

这篇文章介绍了使用Dapper实现CURD操作的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

在上一篇文章中,我解释了如何安装Dapper,本文将解释如何使用Dapper来使用凝乳操作。

示例中使用的实体类定义如下:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace DapperApplicationDemo.Model{ public class User { public int UserId { get; set; } public string UserName { get; set; } public string Email { get; set; } public string Address { get; set; } }}

注意:在使用以下方法之前,先介绍一下Dapper的命名空室:使用Dapper;

一、插入数据1、使用匿名类插入数据IDbConnection connection = new SqlConnection(conn);var result = connection.Execute("Insert into Users values (@UserName, @Email, @Address)",new { UserName = "Tom", Email = "747954712@qq.com", Address = "北京" });

查询数据库:


2、使用实体类插入数据string sqlCommandText = "insert into Users(UserName,Email,Address) Values (@UserName,@Email,@Address)";using (IDbConnection connection = new SqlConnection(conn)){ User user = new User() { UserName = "tim", Email = "78415155@qq.com", Address = "北京" }; int result = connection.Execute(sqlCommandText,user); if (result gt; 0) { Console.WriteLine("插入成功!"); } else { Console.WriteLine("插入失败!"); }}

查询数据库:


3、InsertBulk操作

既然是批量操作,那就一定是批量插入。我们要做的就是使用上面使用的ldquo匿名对象rdquo变成ldquo匿名对象集合rdquo就是这样。代码如下:

using (IDbConnection connection = new SqlConnection(conn)){ var userList = Enumerable.Range(1012, 100000).Select(i =gt; new User() { Email = i + "qq.com", Address = "北京", UserName = "CK" + i, }); var result = connection.Execute("insert into Users values(@UserName,@Email,@Address)", userList);}

查询数据库:


二、查询数据using (IDbConnection connection = new SqlConnection(conn)){ // 查询 var query = connection.Querylt;Usergt;("SELECT * FROM Users"); query.AsList().ForEach(p =gt; { Console.WriteLine("Id:"+p.UserId+" UserName:"+p.UserName+" Email:"+p.Email+" Address:"+p.Address); });}

程序运行结果:


三、更新数据1、使用匿名类更新using (IDbConnection connection = new SqlConnection(conn)){ var result = connection.Execute("update Users set UserName='Tim',Address='上海' where UserId=@UserId", new { UserId = 2 });}

查询数据库:


2、使用实体类更新using (IDbConnection connection = new SqlConnection(conn)){ User user = new User(); user.UserName = "张无忌"; user.UserId = 1; var result = connection.Execute("update Users set UserName=@UserName where UserId=@UserId", user);}

查询数据库:


3、使用键值对更新using (IDbConnection connection = new SqlConnection(conn)){ Listlt;KeyValuePairlt;string, objectgt;gt; keys = new Listlt;KeyValuePairlt;string, objectgt;gt;(); keys.Add(new KeyValuePairlt;string, objectgt;("@UserName", "风清扬")); keys.Add(new KeyValuePairlt;string, objectgt;("@UserId", 2)); var result = connection.Execute("update Users set UserName=@UserName where UserId=@UserId", keys);}

查询数据库:


四、删除数据1、使用匿名类删除数据using (IDbConnection connection = new SqlConnection(conn)){ var result = connection.Execute("delete from Users where UserId=@UserId", new { UserId = 3 });}2、使用实体类删除数据using (IDbConnection connection = new SqlConnection(conn)){ User user = new User(); user.UserId = 4; var result = connection.Execute("delete from Users where UserId=@UserId", user);}

示例程序代码的下载地址:单击此处下载

关于使用Dapper实现凝乳操作的文章到此结束。希望对大家的学习有所帮助,

0

精彩评论

暂无评论...
验证码 换一张
取 消