最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

輕量級ORM框架Dapper應(yīng)用之實現(xiàn)CURD操作

 更新時間:2022年03月05日 16:30:04   作者:.NET開發(fā)菜鳥  
這篇文章介紹了使用Dapper實現(xiàn)CURD操作的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

上一篇文章中,講解了如何安裝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;

一、插入數(shù)據(jù)

1、使用匿名類插入數(shù)據(jù)

IDbConnection connection = new SqlConnection(conn);
var result = connection.Execute(
"Insert into Users values (@UserName, @Email, @Address)",
new { UserName = "Tom", Email = "747954712@qq.com", Address = "北京" });

查詢數(shù)據(jù)庫:

2、使用實體類插入數(shù)據(jù)

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("插入失敗!");
      }
}

查詢數(shù)據(jù)庫:

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);
}

查詢數(shù)據(jù)庫:

二、查詢數(shù)據(jù)

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);
        });
}

程序運行結(jié)果:

三、更新數(shù)據(jù)

1、使用匿名類更新

using (IDbConnection connection = new SqlConnection(conn))
{
       var result = connection.Execute("update Users set UserName='Tim',Address='上海' where UserId=@UserId", new { UserId = 2 });
}

查詢數(shù)據(jù)庫:

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);
}

查詢數(shù)據(jù)庫:

3、使用鍵值對更新

using (IDbConnection connection = new SqlConnection(conn))
{
       List<KeyValuePair<string, object>> keys = new List<KeyValuePair<string, object>>();
       keys.Add(new KeyValuePair<string, object>("@UserName", "風(fēng)清揚"));
       keys.Add(new KeyValuePair<string, object>("@UserId", 2));
       var result = connection.Execute("update Users set UserName=@UserName where UserId=@UserId", keys);
}

查詢數(shù)據(jù)庫:

四、刪除數(shù)據(jù)

1、使用匿名類刪除數(shù)據(jù)

using (IDbConnection connection = new SqlConnection(conn))
{
       var result = connection.Execute("delete from Users where UserId=@UserId", new { UserId = 3 });
}

2、使用實體類刪除數(shù)據(jù)

using (IDbConnection connection = new SqlConnection(conn))
{
        User user = new User();
        user.UserId = 4;
        var result = connection.Execute("delete from Users where UserId=@UserId", user);
}

示例程序代碼下載地址:點此下載

到此這篇關(guān)于使用Dapper實現(xiàn)CURD操作的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • .net6簡單使用NPOI讀取Excel的項目實踐

    .net6簡單使用NPOI讀取Excel的項目實踐

    本文主要介紹了.net6簡單使用NPOI讀取Excel的項目實踐,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • Entity Framework常用查詢語句

    Entity Framework常用查詢語句

    這篇文章介紹了Entity Framework常用查詢語句的用法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-03-03
  • C#.net 微信公眾賬號接口開發(fā)

    C#.net 微信公眾賬號接口開發(fā)

    這篇文章主要介紹了C#.net 微信公眾賬號接口開發(fā),需要的朋友可以參考下
    2016-05-05
  • Asp.net_Table控件の單元格縱向合并示例

    Asp.net_Table控件の單元格縱向合并示例

    本文為大家介紹下如何實現(xiàn)動態(tài)生成表,同一列中數(shù)據(jù)相同的單元格需要合并,具體實現(xiàn)如下,由此需求的朋友可以參考下,希望對大家有所幫助
    2013-07-07
  • .Net Core使用MongoDB的詳細(xì)教程

    .Net Core使用MongoDB的詳細(xì)教程

    這篇文章主要給大家介紹了關(guān)于.Net Core使用MongoDB的完整步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • asp.net直接Response輸出WML頁面示例代碼

    asp.net直接Response輸出WML頁面示例代碼

    本例實現(xiàn)直接Response輸出WML頁面,具體代碼如下,有需要的朋友可以和參考下
    2013-08-08
  • .NET Core中本地化機(jī)制的深入講解

    .NET Core中本地化機(jī)制的深入講解

    這篇文章主要給大家介紹了關(guān)于.NET Core中本地化機(jī)制的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-10-10
  • asp.net DataGridView導(dǎo)出到Excel的三個方法[親測]

    asp.net DataGridView導(dǎo)出到Excel的三個方法[親測]

    打開Excel并將DataGridView控件中數(shù)據(jù)導(dǎo)出到Excel的幾種方法
    2008-08-08
  • ASP.NET筆記之 控件與母板的區(qū)別分析

    ASP.NET筆記之 控件與母板的區(qū)別分析

    本篇文章小編為大家介紹,ASP.NET筆記之 控件與母板的區(qū)別分析。需要的朋友參考下
    2013-04-04
  • asp.net 存儲過程調(diào)用

    asp.net 存儲過程調(diào)用

    調(diào)用存儲過程,但無返回值 調(diào)用存儲過程,返回普通值 調(diào)用存儲過程,返回數(shù)據(jù)集的實現(xiàn)代碼。
    2009-07-07

最新評論

什邡市| 山东| 沈阳市| 巴青县| 沙雅县| 高密市| 广宁县| 师宗县| 岚皋县| 广宁县| 柳林县| 卢氏县| 衡阳市| 道真| 阜平县| 天等县| 文水县| 郧西县| 克什克腾旗| 长寿区| 兴仁县| 元朗区| 韩城市| 额敏县| 迁西县| 上思县| 建始县| 赤城县| 灌云县| 麻城市| 营口市| 新巴尔虎左旗| 嘉黎县| 吉木乃县| 木兰县| 甘泉县| 永定县| 拉孜县| 汶川县| 金坛市| 张掖市|