In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to use Dapper CURD operation". In daily operation, I believe many people have doubts about how to use Dapper CURD operation. Xiaobian consulted various materials and sorted out simple and easy operation methods. I hope to help you answer the doubts about "how to use Dapper CURD operation"! Next, please follow the small series to learn together!
The entity classes used in the example are defined as follows:
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; } }}
Note: First introduce Dapper namespace before using the following methods: Using Dapper;
1. Insert data 1. Insert data using anonymous class IDbConnection = new SqlConnection(conn);var result = connection.Execute("Insert into Users values (@UserName, @Email, @Address)",new { UserName = "Tom", Email = "747954712@qq.com", Address = "Beijing" });
Query database:
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 = "Beijing" }; int result = connection.Execute(sqlCommandText,user); if (result > 0) { Console.WriteLine("Insert successful! "); } else { Console.WriteLine("Insert failed! "); }}
Query database:
3、InsertBulk操作
既然是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("SELECT * FROM Users"); query.AsList().ForEach(p => { 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)){ List keys = new List(); keys.Add(new KeyValuePair("@UserName", "风清扬")); keys.Add(new KeyValuePair("@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 CURD操作"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.