C# SqlSugar框架的學習使用(四)-- 插入數據的詳細用法

前言web

上一篇《C# SqlSugar框架的學習使用(三)-- 查詢的多種用法》咱們已經把SqlSugar的查詢多種用法實現了,這篇咱們就來講說插入數據的多種用法。數據庫


數據源微信

數據表  POSTemp框架

咱們仍是用前面介紹的數據庫,由於此次插入時要講一下存在自增加類型的數據,因此咱們如今用POSTemp這個表,結構以下圖:ide


程序SqlSugarTest
佈局

程序中加入插入的功能按鈕等,以下圖:性能


POSTemp類學習

using System;using System.Linq;using System.Text;using SqlSugar;
namespace Model{ ///<summary> /// ///</summary> public partial class POSTemp { public POSTemp() {

} /// <summary> /// /// </summary> [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] public int sn { get; set; }
/// <summary> /// Desc: /// Default: /// Nullable:False /// </summary> public string Posno { get; set; }
/// <summary> /// Desc: /// Default: /// Nullable:False /// </summary> public string Posname { get; set; }
/// <summary> /// Desc: /// Default: /// Nullable:True /// </summary> public string OrgCode { get; set; }
/// <summary> /// Desc: /// Default: /// Nullable:True /// </summary> public string status { get; set; }
}}


代碼演示

插入返回影響行數ui

 private void tsmnuinsertcount_Click(object sender, EventArgs e) { try { POSTemp pos = new POSTemp(); pos.Posno = "0001"; pos.Posname = "01款臺"; pos.OrgCode = "001"; pos.status = "1";
int count = _db.Insertable(pos).ExecuteCommand(); TextShow("插入了" + count + "條數據"); } catch (Exception ex) { TextShow(ex.Message); } }


插入返回自增列
url

 private void tsmnuinsertident_Click(object sender, EventArgs e) { try { POSTemp pos = new POSTemp(); pos.Posno = "0002"; pos.Posname = "02款臺"; pos.OrgCode = "001"; pos.status = "1";
int count = _db.Insertable(pos).ExecuteReturnIdentity(); TextShow("插入數據成功,自增列序號爲:" + count ); } catch (Exception ex) { TextShow(ex.Message); } }


插入返回實體

 private void tsmnuinsertcls_Click(object sender, EventArgs e) { try { POSTemp pos = new POSTemp(); pos.Posno = "0003"; pos.Posname = "03款臺"; pos.OrgCode = "001"; pos.status = "1";
POSTemp retPosTemp = _db.Insertable(pos).ExecuteReturnEntity(); TextShow("插入數據成功"); TextShow("sn:" + retPosTemp.sn + " Posno:" + retPosTemp.Posno + " PosName:" + retPosTemp.Posname); TextShow("OrgCode:" + retPosTemp.OrgCode + " status:" + retPosTemp.status); } catch (Exception ex) { TextShow(ex.Message); } }


只插入對應列

 private void tsmnuinsertcolumn_Click(object sender, EventArgs e) { try { POSTemp pos = new POSTemp(); pos.Posno = "0004"; pos.Posname = "04款臺"; pos.OrgCode = "001"; pos.status = "1";
int count = _db.Insertable(pos) .InsertColumns(t => new {t.Posno, t.Posname}) .ExecuteReturnIdentity(); TextShow("只插對應列數據成功,自增列序號爲:" + count); } catch (Exception ex) { TextShow(ex.Message); } }


不插入對應列

 private void tsmnuinsertignore_Click(object sender, EventArgs e) { try { POSTemp pos = new POSTemp(); pos.Posno = "0005"; pos.Posname = "05款臺"; pos.OrgCode = "001"; pos.status = "1";
int count = _db.Insertable(pos) .IgnoreColumns(t => new { t.OrgCode }) .ExecuteReturnIdentity(); TextShow("不插對應列數據成功,自增列序號爲:" + count); } catch (Exception ex) { TextShow(ex.Message); } }


根據條件指定不插入列

 private void tsmnuinsertignorewhere_Click(object sender, EventArgs e) { try { POSTemp pos = new POSTemp(); pos.Posno = "0006"; pos.Posname = "06款臺"; pos.OrgCode = "001"; pos.status = "1";
int count = _db.Insertable(pos) .IgnoreColumns(t => t == "status") .ExecuteReturnIdentity(); TextShow("根據條件不插對應列數據成功,自增列序號爲:" + count); } catch (Exception ex) { TextShow(ex.Message); } }


List中全部列不插入

 private void tsmnuinsertignorelist_Click(object sender, EventArgs e){ try { POSTemp pos = new POSTemp(); pos.Posno = "0007"; pos.Posname = "07款臺"; pos.OrgCode = "001"; pos.status = "1";
List<string> list= new List<string>(); list.Add("OrgCode"); list.Add("status");
int count = _db.Insertable(pos) .IgnoreColumns(t => list.Contains(t)) .ExecuteReturnIdentity(); TextShow("根據條件不插對應列數據成功,自增列序號爲:" + count); } catch (Exception ex) { TextShow(ex.Message); } }


插入使用鎖

 private void tsmnuinsertlock_Click(object sender, EventArgs e) { try { POSTemp pos = new POSTemp(); pos.Posno = "0008"; pos.Posname = "08款臺"; pos.OrgCode = "001"; pos.status = "1";
int count = _db.Insertable(pos) .With(SqlWith.UpdLock) .ExecuteReturnIdentity(); TextShow("插入使用鎖數據成功,自增列序號爲:" + count); } catch (Exception ex) { TextShow(ex.Message); } }


批量插入(性能很快不用操心)

 private void tsmnuinsertarray_Click(object sender, EventArgs e) { try { List<POSTemp> listpos = new List<POSTemp>(); for (int i = 0; i < 3; i++) { POSTemp pos = new POSTemp(); pos.Posno = "001"+i; pos.Posname = pos.Posno + "款臺"; pos.OrgCode = "001"; pos.status = "1"; listpos.Add(pos); }
int count = _db.Insertable(listpos.ToArray()) .ExecuteCommand(); TextShow("List插入數據,成功插入" + count + "條數據"); } catch (Exception ex) { TextShow(ex.Message); } }


匿名對象和字典的支持

 private void tsmnuinsertdictionary_Click(object sender, EventArgs e) { try { int count = _db.Insertable<POSTemp>( new { Posno = "0013", Posname = "13款臺" }) .ExecuteReturnIdentity(); TextShow("匿名對插成功插入成功,自增序號爲:" + count);
count = _db.Insertable<POSTemp>( new Dictionary<string, object>() { { "Posno","0014"}, {"Posname","14款臺" } }) .ExecuteReturnIdentity(); TextShow("字典方式插成功插入成功,自增序號爲:" + count); } catch (Exception ex) { TextShow(ex.Message); } }


將A表數據插入B表

咱們將POSTemp的表數據插入到POS中,先看下圖,POSTemp中有13條數據,POS中沒有數據


插入代碼

 private void tsmnuinsertAtoB_Click(object sender, EventArgs e) { try { int count = _db.Insertable(_db.Queryable<POSTemp>() .Select<POS>().ToList()).ExecuteCommand(); TextShow("從POSTemp表中向POS表中成功插入了" + count + "條數據"); } catch (Exception ex) { TextShow(ex.Message); } }





-END-



Vaccae的往期經典


OpenCV

《C++ OpenCV案例實戰---卡號獲取

《C++ OpenCV案例實戰---卡片截取(附代碼)

《C++ OpenCV透視變換---切換手機正面圖片》

《C++ OpenCV實戰---獲取數量

《C++ OpenCV實戰---利用顏色分割獲取數量

OpenCV4Android NDK方式進行Canny邊緣檢測》

《OpenCV4Android NDK方式TesserartOCR實時進行識別

《OpenCV4Android NDK級聯方式實時進行人臉檢測》


Android

《Android利用SurfaceView結合科大訊飛修改語音實別UI

《Android關於語音識別的功能實現分析(一)---結構化思惟》

《Android關於語音識別的功能實現分析(二)---語義解析》

《Android根據類生成簽名字符串

《Android碎片化佈局fragment的實戰應用

《Android中RecyclerView嵌套RecyclerView

《Android裏用AsyncTask後的接口回調


.Net C#

《C#自定義特性(Attribute)講解與實際應用

《C#根據類生成簽名字符串(附DEMO下載地址)

《C++建立動態庫C#調用》

《C#與三菱PLC(型號FX2N)串口通信類


數據庫及其它

《Oracel存儲過程寫報表實戰》

《Delphi輪播視頻和圖片程序(用於雙屏顯示程序)

《SQL隨機增長銷售數據的腳本編寫(附腳本下載地址)

SQL Server中With As的介紹與應用(三)--遞歸的實戰應用

《Oracle經過ODBC鏈接SQL Server數據庫

Oracle利用row_number()over()方式解決插入數據時重複鍵的問題



長按下方二維碼關注微卡智享




本文分享自微信公衆號 - 微卡智享(VaccaeShare)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。

相關文章
相關標籤/搜索