前言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> [ ] 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
《OpenCV4Android NDK方式进行Canny边缘检测》
《OpenCV4Android NDK方式TesserartOCR实时进行识别》
《OpenCV4Android NDK级联方式实时进行人脸检测》
Android
《Android利用SurfaceView结合科大讯飞修改语音实别UI》
《Android关于语音识别的功能实现分析(一)---结构化思惟》
《Android关于语音识别的功能实现分析(二)---语义解析》
《Android中RecyclerView嵌套RecyclerView》
.Net C#
数据库及其它
《SQL Server中With As的介绍与应用(三)--递归的实战应用》
《Oracle利用row_number()over()方式解决插入数据时重复键的问题》
长按下方二维码关注微卡智享
本文分享自微信公众号 - 微卡智享(VaccaeShare)。
若有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一块儿分享。