首頁 > 軟體

輕量級ORM框架Dapper應用之實現CURD操作

2022-03-05 19:00:32

上一篇文章中,講解了如何安裝Dapper,這篇文章中將會講解如何使用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;

一、插入資料

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 > 0)
      {
          Console.WriteLine("插入成功!");
      }
      else
      {
          Console.WriteLine("插入失敗!");
      }
}

查詢資料庫:

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<User>("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<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);
}

查詢資料庫:

四、刪除資料

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操作的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支援it145.com。


IT145.com E-mail:sddin#qq.com