前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C# SqlSugar框架的学习使用(四)-- 插入数据的详细用法

C# SqlSugar框架的学习使用(四)-- 插入数据的详细用法

作者头像
Vaccae
发布2019-08-20 16:54:35
9K0
发布2019-08-20 16:54:35
举报
文章被收录于专栏:微卡智享

前言

上一篇《C# SqlSugar框架的学习使用(三)-- 查询的多种用法》我们已经把SqlSugar的查询多种用法实现了,这篇我们就来说说插入数据的多种用法。

数据源

数据表 POSTemp

我们还是用前面介绍的数据库,因为这次插入时要讲一下存在自增长类型的数据,所以我们现在用POSTemp这个表,结构如下图:

程序SqlSugarTest

程序中加入插入的功能按钮等,如下图:

POSTemp类

代码语言:javascript
复制
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; }

    }
}

代码演示

插入返回影响行数

代码语言:javascript
复制
     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);
            }
        }

插入返回自增列

代码语言:javascript
复制
        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);
            }
        }

插入返回实体

代码语言:javascript
复制
        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);
            }
        }

只插入对应列

代码语言:javascript
复制
        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);
            }
        }

不插入对应列

代码语言:javascript
复制
       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);
            }
        }

根据条件指定不插入列

代码语言:javascript
复制
        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中所有列不插入

代码语言:javascript
复制
        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);
            }
        }

插入使用锁

代码语言:javascript
复制
        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);
            }
        }

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

代码语言:javascript
复制
        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);
            }
        }

匿名对象和字典的支持

代码语言:javascript
复制
        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中没有数据

插入代码

代码语言:javascript
复制
        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-

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-08-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 微卡智享 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档