Dapper基本用法--MySql

来源:https://www.cnblogs.com/HYJ0201/archive/2020/05/17/12905719.html
-Advertisement-
Play Games

NuGet: Dapper 2.0.35 MySql.Data System.Data 實體(Entity) 1 public class student 2 { 3 public int Id { get; set; } 4 public string RealName { get; set; } ...


NuGet:

  • Dapper 2.0.35
  • MySql.Data
  • System.Data

實體(Entity)

1    public class student
2     {
3         public int Id { get; set; }
4         public string RealName { get; set; }
5         public DateTime EnrollmenDate { get; set; }
6     }

 

配置文件資料庫連接

  <connectionStrings>
    <add name="mysqlConn" connectionString="server=127.0.0.1;database=資料庫;User ID=root;password=密碼"/>
  </connectionStrings>

 

MySql連接字元串

1 private static string connStr = string.Empty;
2         public MySqlOperation()
3         {
4             connStr = System.Configuration.ConfigurationManager.ConnectionStrings["mysqlConn"].ConnectionString;
5         }

查詢語句

 1         /// <summary>
 2         /// 查詢所有
 3         /// </summary>
 4         /// <returns>lists</returns>
 5         public List<student> Query()
 6         {
 7             if (string.IsNullOrEmpty(connStr))
 8             {
 9                 return new List<student>(); ;
10             }
11             try
12             {
13                 using (IDbConnection db = new MySqlConnection(connStr))
14                 {
15                     db.Open();//打開資料庫
16                     string mysqlStr = "select * from  student";
17                     List<student> lists = db.Query<student>(mysqlStr).ToList();
18                     db.Close();//關閉資料庫
19                     return lists;
20                 }
21             }
22             catch (Exception)
23             {
24                 return new List<student>();
25             }
26         }
27         /// <summary>
28         /// 根據Id查詢
29         /// </summary>
30         /// <param name="id"></param>
31         /// <returns>lists</returns>
32         public List<student> Query(string id)
33         {
34             if (string.IsNullOrEmpty(connStr))
35             {
36                 return new List<student>();
37             }
38             try
39             {
40                 using (IDbConnection db = new MySqlConnection(connStr))
41                 {
42                     db.Open();//打開資料庫
43                     string mysqlStr = "select * from  student where Id=@id";
44                     List<student> lists = db.Query<student>(mysqlStr, new { Id = id }).ToList();
45                     db.Close();//關閉資料庫
46                     return lists;
47                 }
48             }
49             catch (Exception)
50             {
51                 return new List<student>();
52             }
53         }

 

插入語句

 1         /// <summary>
 2         /// 插入數據
 3         /// </summary>
 4         /// <param name="student">student</param>
 5         /// <returns>-1:連接字元串為空,-2:新增失敗</returns>
 6         public int Insert(student student)
 7         {
 8             if (string.IsNullOrEmpty(connStr))
 9             {
10                 return -1;
11             }
12             try
13             {
14                 using (IDbConnection db = new MySqlConnection(connStr))
15                 {
16                     db.Open();//打開資料庫
17                     string mysqlStr = "insert into student(Id,RealName,EnrollmenDate) values(@Id,@RealName,@EnrollmenDate)";
18                     int list = db.Execute(mysqlStr, student);
19                     db.Close();//關閉資料庫
20                     return list;
21                 }
22             }
23             catch (Exception)
24             {
25                 return -2;
26             }
27         }

 

刪除數據

 1         /// <summary>
 2         /// 刪除數據
 3         /// </summary>
 4         /// <param name="id"></param>
 5         /// <returns></returns>
 6         public int Delect(string id)
 7         {
 8             if (string.IsNullOrEmpty(connStr))
 9             {
10                 return -1;
11             }
12             try
13             {
14                 using (IDbConnection db = new MySqlConnection(connStr))
15                 {
16                     db.Open();//打開資料庫
17                     string mysqlStr = "delete from student where Id=@id";
18                     int list = db.Execute(mysqlStr, new { Id = id });
19                     db.Close();//關閉資料庫
20                     return list;
21                 }
22             }
23             catch (Exception)
24             {
25                 return -2;
26             }
27         }

更新數據

 1         /// <summary>
 2         /// 更新信息
 3         /// </summary>
 4         /// <param name="student"></param>
 5         /// <returns>-1:連接字元串為空,-2:更新失敗</returns>
 6         public int Update(student student)
 7         {
 8             if (string.IsNullOrEmpty(connStr))
 9             {
10                 return -1;
11             }
12             try
13             {
14                 using (IDbConnection db = new MySqlConnection(connStr))
15                 {
16                     db.Open();//打開資料庫
17                     string mysqlStr = "update student set RealName=@RealName where Id=@Id";
18                     int list = db.Execute(mysqlStr, student);
19                     db.Close();//關閉資料庫
20                     return list;
21                 }
22             }
23             catch (Exception)
24             {
25                 return -2;
26             }
27         }

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 如何使用Spring Boot發送郵件? Spring Boot為發送郵件提供了starter:spring-boot-starter-mail 。 接下來,我們看看如何用Spring Boot發送郵件。 一、配置郵箱 這裡我們使用163網易郵箱 1.開啟SMTP服務 2.設置/重置客戶端授權密碼 ...
  • 如果因為過早學一樣東西而有損後來的學習成長,那這種學習無異於“揠苗助長”。正是基於這個原因,教會小學生理解x=x+1是有害無益的。 ...
  • Spring Boot項目啟動的時候會列印如下內容。 1 . ____ _ __ _ _ 2 /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ 3 ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ 4 \\/ ___)| |_)| | | ...
  • https://www.wenjuan.com/s/mQf2uaH/https://www.wenjuan.com/s/mQf2uaHhttps://www.wenjuan.com/z/mQf2uaH/https://www.wenjuan.com/z/mQf2uaHhttps://www.wenj ...
  • 在上面文章abp(net core)+easyui+efcore實現倉儲管理系統——入庫管理之十一(四十七) 的學習之後,我們已經實現了入庫單前端的關於實現庫位的功能,今天我們來學習如何在後臺實現添加庫位的功能。接上文。 ...
  • C#硬體開發,一種是調用廠家提供的api;另一種就是通過com口,發送命令,和硬體通信。這2種方法,如果有硬體,業務流程很好調試。但是大部分硬體,只有和客戶聯調才會有硬體調試的機會。那業務流程沒有硬體,怎樣調試?一種是利用vs自帶的斷點跳過功能,這種方式慢,而且不同人調試都要加斷點,0效率低。另一種 ...
  • 上一篇:異常Exception(二) 使用try...catch...捕獲異常,如果能預料到某些異常可能發生,可以使用精確的異常例如“FileNotFoundException”、“DirectoryNotFoundException”、“IOException”等,最有使用一般異常“Excepti ...
  • 《ASP.NET MVC 5 編程實戰》 [作者] (美) Dino Esposito[譯者] (中) 潘麗臣[出版] 清華大學出版社[版次] 2015年03月 第1版[印次] 2015年03月 第1次 印刷[定價] 59.80元 【前言】 Web Forms 的最常見應用場景是,你要開發專註於呈現 ...
一周排行
    -Advertisement-
    Play Games
  • .Net8.0 Blazor Hybird 桌面端 (WPF/Winform) 實測可以完整運行在 win7sp1/win10/win11. 如果用其他工具打包,還可以運行在mac/linux下, 傳送門BlazorHybrid 發佈為無依賴包方式 安裝 WebView2Runtime 1.57 M ...
  • 目錄前言PostgreSql安裝測試額外Nuget安裝Person.cs模擬運行Navicate連postgresql解決方案Garnet為什麼要選擇Garnet而不是RedisRedis不再開源Windows版的Redis是由微軟維護的Windows Redis版本老舊,後續可能不再更新Garne ...
  • C#TMS系統代碼-聯表報表學習 領導被裁了之後很快就有人上任了,幾乎是無縫銜接,很難讓我不想到這早就決定好了。我的職責沒有任何變化。感受下來這個系統封裝程度很高,我只要會調用方法就行。這個系統交付之後不會有太多問題,更多應該是做小需求,有大的開發任務應該也是第二期的事,嗯?怎麼感覺我變成運維了?而 ...
  • 我在隨筆《EAV模型(實體-屬性-值)的設計和低代碼的處理方案(1)》中介紹了一些基本的EAV模型設計知識和基於Winform場景下低代碼(或者說無代碼)的一些實現思路,在本篇隨筆中,我們來分析一下這種針對通用業務,且只需定義就能構建業務模塊存儲和界面的解決方案,其中的數據查詢處理的操作。 ...
  • 對某個遠程伺服器啟用和設置NTP服務(Windows系統) 打開註冊表 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpServer 將 Enabled 的值設置為 1,這將啟用NTP伺服器功 ...
  • title: Django信號與擴展:深入理解與實踐 date: 2024/5/15 22:40:52 updated: 2024/5/15 22:40:52 categories: 後端開發 tags: Django 信號 松耦合 觀察者 擴展 安全 性能 第一部分:Django信號基礎 Djan ...
  • 使用xadmin2遇到的問題&解決 環境配置: 使用的模塊版本: 關聯的包 Django 3.2.15 mysqlclient 2.2.4 xadmin 2.0.1 django-crispy-forms >= 1.6.0 django-import-export >= 0.5.1 django-r ...
  • 今天我打算整點兒不一樣的內容,通過之前學習的TransformerMap和LazyMap鏈,想搞點不一樣的,所以我關註了另外一條鏈DefaultedMap鏈,主要調用鏈為: 調用鏈詳細描述: ObjectInputStream.readObject() DefaultedMap.readObject ...
  • 後端應用級開發者該如何擁抱 AI GC?就是在這樣的一個大的浪潮下,我們的傳統的應用級開發者。我們該如何選擇職業或者是如何去快速轉型,跟上這樣的一個行業的一個浪潮? 0 AI金字塔模型 越往上它的整個難度就是職業機會也好,或者說是整個的這個運作也好,它的難度會越大,然後越往下機會就會越多,所以這是一 ...
  • @Autowired是Spring框架提供的註解,@Resource是Java EE 5規範提供的註解。 @Autowired預設按照類型自動裝配,而@Resource預設按照名稱自動裝配。 @Autowired支持@Qualifier註解來指定裝配哪一個具有相同類型的bean,而@Resourc... ...