这篇文章主要介绍“如何使用Dapper CURD操作”,在日常操作中,相信很多人在如何使用Dapper CURD操作问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”如何使用Dapper CURD操作”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
例子中使用到的实体类定义如下:
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的命名空间:Using Dapper;
IDbConnection connection = new SqlConnection(conn); var result = connection.Execute( "Insert into Users values (@UserName, @Email, @Address)", new { UserName = "Tom", Email = "747954712@qq.com", Address = "北京" });
查询数据库:
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 > 0) { Console.WriteLine("插入成功!"); } else { Console.WriteLine("插入失败!"); } }
查询数据库:
既然是Bulk操作,那肯定就是批量插入了,我们要做的就是将上面使用到的“匿名对象”变成“匿名对象集合”就可以了,代码如下:
using (IDbConnection connection = new SqlConnection(conn)) { var userList = Enumerable.Range(1012, 100000).Select(i => 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.Query<User>("SELECT * FROM Users"); query.AsList().ForEach(p => { Console.WriteLine("Id:"+p.UserId+" UserName:"+p.UserName+" Email:"+p.Email+" Address:"+p.Address); }); }
程序运行结果:
using (IDbConnection connection = new SqlConnection(conn)) { var result = connection.Execute("update Users set UserName='Tim',Address='上海' where UserId=@UserId", new { UserId = 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); }
查询数据库:
using (IDbConnection connection = new SqlConnection(conn)) { List<KeyValuePair<string, object>> keys = new List<KeyValuePair<string, object>>(); keys.Add(new KeyValuePair<string, object>("@UserName", "风清扬")); keys.Add(new KeyValuePair<string, object>("@UserId", 2)); var result = connection.Execute("update Users set UserName=@UserName where UserId=@UserId", keys); }
查询数据库:
using (IDbConnection connection = new SqlConnection(conn)) { var result = connection.Execute("delete from Users where UserId=@UserId", new { UserId = 3 }); }
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 CURD操作”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。