您好,登錄后才能下訂單哦!
這篇文章主要介紹“如何使用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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。