您当前的位置: 首页 >  ar

txwtech

暂无认证

  • 5浏览

    0关注

    813博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

SqlSugar库操作mysql数据库方法-增删改查

txwtech 发布时间:2022-07-04 16:45:56 ,浏览量:5

SqlSugar库操作mysql数据库方法

 添加引用:

MySql.Data.dll,SqlSugar.dll

增删改查

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SqlSugar_MysqlTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button_mysql_test_Click(object sender, EventArgs e)
        {
            DBContext aa= DBContext.OpDB();
           
            Expression < Func> cd = null;

            string insert_str = "sn_202207043";
            //  List list=aa.GetList(cd=f=>f.id=="10009"&&f.lot=="123456"); //多条件查询
            //查询
            List list = aa.GetList(cd = f => f.sn_box== insert_str);
            for (int i=0;i0)
            {
               // MessageBox.Show("已经存在物流码"+ insert_str);
               // return;
            }
            //by txwtech
            else
            {
                //增加
              //  aa.Insert(new Sugar_Get_Info_Class { id = "10011", lot = "", sn_box = insert_str });
            }
            //删除
            aa.Delete(cd = f => f.id == "10008");
            //public virtual bool Update(Expression columns, Expression whereExpression);

            // Update(Expression < Func < T, T >>  执行的动作,返回的结果类型
            //Update(Expression  执行的动作,返回的结果类型

            //修改
            aa.Update(p=>new Sugar_Get_Info_Class {base_data="99.0",lot="LA123456" },cd=f=>f.id=="10009");




        }
    }
}

DBContext.cs 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
//using AgvDisPatch.Config;
//by txwtech
using SqlSugar;
namespace SqlSugar_MysqlTest
{
    public class DBContext where T : class, new()
    {
        public SqlSugarClient Db;
        /// 
        /// 单列模式
        /// 
        private static DBContext mSingle = null;
        public static DBContext GetInstance()
        {
            if (mSingle == null)
                mSingle = new DBContext();
            return mSingle;
        }

        /// 
        /// 修改后的代码
        /// 
        /// 
        public static DBContext OpDB()
        {
            DBContext dbcontext_t = new DBContext();
            dbcontext_t.Db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = "database='" + "txw_oqa_db" + "';Data Source = '" + "127.0.0.1" + "'; User Id = '" + "root" + "'; pwd='" + "87958868" + "';charset='utf8';pooling=true",
                //DbType = SqlSugar.DbType.MySql,
                //IsAutoCloseConnection = true,
                //InitKeyType = InitKeyType.Attribute

               // ConnectionString = ConfigFile.DataConnString,
                DbType = SqlSugar.DbType.MySql,//我这里使用的是Mysql数据库
                IsAutoCloseConnection = true,//自动关闭连接
                InitKeyType = InitKeyType.Attribute

            });
            return dbcontext_t;
        }





        protected DBContext()
        {  //通过这个可以直接连接数据库
            Db = new SqlSugarClient(new ConnectionConfig()
            {
                // "database='" + "return" + "';Data Source = '" + "127.0.0.1" + "'; User Id = '" + "root" + "'; pwd='" + "root" + "';charset='utf8';pooling=true",
                //可以在连接字符串中设置连接池pooling=true;表示开启连接池
                //eg:min pool size=2;max poll size=4;表示最小连接池为2,最大连接池是4;默认是100
                ConnectionString = "database='" + "txw_oqa_db" + "';Data Source = '" + "127.0.0.1" + "'; User Id = '" + "root" + "'; pwd='" + "87958868" + "';charset='utf8';pooling=true",
                // ConnectionString = ConfigFile.DataConnString,
                DbType = SqlSugar.DbType.MySql,//我这里使用的是Mysql数据库
                IsAutoCloseConnection = true,//自动关闭连接
                InitKeyType = InitKeyType.Attribute
            });
        }
        public void Dispose()
        {
            if (Db != null)
            {
                Db.Dispose();
            }
        }
        public SimpleClient CurrentDb { get { return new SimpleClient(Db); } }

        /// 
        /// 获取所有
        /// 
        /// 
        public virtual List GetList()
        {
            return CurrentDb.GetList();
        }

        /// 
        /// 根据表达式查询
        /// 
        /// 
        public virtual List GetList(Expression whereExpression)
        {
            //db.Queryable().Where(it => it.Name != null).ToList()
            return CurrentDb.GetList(whereExpression);
        }


        /// 
        /// 根据表达式查询分页
        /// 
        /// 
        public virtual List GetPageList(Expression whereExpression, PageModel pageModel)
        {
            return CurrentDb.GetPageList(whereExpression, pageModel);
        }

        /// 
        /// 根据表达式查询分页并排序
        /// 
        /// it
        /// 
        /// it=>it.id或者it=>new{it.id,it.name}
        /// OrderByType.Desc
        /// 
        public virtual List GetPageList(Expression whereExpression, PageModel pageModel, Expression orderByExpression = null, OrderByType orderByType = OrderByType.Asc)
        {
            return CurrentDb.GetPageList(whereExpression, pageModel, orderByExpression, orderByType);
        }


        /// 
        /// 根据主键查询
        /// 
        /// 
        public virtual List GetById(dynamic id)
        {
            return CurrentDb.GetById(id);
        }

        /// 
        /// 根据主键删除
        /// 
        /// 
        /// 
        public virtual bool Delete(dynamic id)
        {
            if (string.IsNullOrEmpty(id.ObjToString))
            {
                Console.WriteLine(string.Format("要删除的主键id不能为空值!"));
            }
            return CurrentDb.Delete(id);
        }


        /// 
        /// 根据实体删除
        /// 
        /// 
        /// 
        public virtual bool Delete(T data)
        {
            if (data == null)
            {
                Console.WriteLine(string.Format("要删除的实体对象不能为空值!"));
            }
            return CurrentDb.Delete(data);
        }

        /// 
        /// 根据主键删除
        /// 
        /// 
        /// 
        public virtual bool Delete(dynamic[] ids)
        {
            if (ids.Count()  0;
        }

        /// 
        /// 根据表达式删除
        /// 
        /// 
        /// 
        public virtual bool Delete(Expression whereExpression)
        {
            return CurrentDb.Delete(whereExpression);
        }


        /// 
        /// 根据实体更新,实体需要有主键
        /// 
        /// 
        /// 
        public virtual bool Update(T obj)
        {
            if (obj == null)
            {
                Console.WriteLine(string.Format("要更新的实体不能为空,必须带上主键!"));
            }
            return CurrentDb.Update(obj);
        }

        /// 
        ///批量更新
        /// 
        /// 
        /// 
        public virtual bool Update(List objs)
        {
            if (objs.Count             
关注
打赏
1665060526
查看更多评论
0.1112s