.Net Core+mySqlSugar的一些稍复杂操作

介绍一些我尝试的mysqlSugar的数据库操作

修改密码

var status =  db.Update<Users>(new { password = user.password }, it => it.username == user.username);

更新数据(防止空值一起更新)

首先封装一个函数(判断是否为空或者数值为0),当然sqlsugar有自己的查询函数,判断值不为空且大于0  SqlFunc.HasNumber(object thisValue)

        public  void ForeachDisableColumns<T>(T model)
        {
            Type t = model.GetType();
            PropertyInfo[] PropertyList = t.GetProperties();
            foreach (PropertyInfo item in PropertyList)
            {
                string name = item.Name;
                object value = item.GetValue(model);
                if (value == null || value.ToString() == "0")
                {
                    db.AddDisableUpdateColumns(name);//新语法添加禁止更新列
                }

            }
        }

则结合以上函数,更新数据的用法

       ForeachDisableColumns(user);
       db.Update<Users>(user, it => it.userid == user.userid);

批量更新,同样使用上述封装的方法

            string strArr = "[{'name':'Kotonami','userid':'1'},{'name':'Akinama','userid':'17'},{'name':'Momozawa','userid':'6'}]";

            List<Users> newuser = JsonConvert.DeserializeObject<List<Users>>(strArr);
            //批量更新   数据量小时建议使用大于十条用SqlBulkReplace
            foreach (var item in newuser)
            {
                ForeachDisableColumns(item);
            }
            db.UpdateRange(newuser);
            db.DisableUpdateColumns = null;//清空禁止更新

返回行数

            var list5 = db.SqlQuery<int>("select userid from Users limit 0,1").SingleOrDefault();

跨表查询,查询某班级学生的姓名和学号

         //方法1
            outStr = db.SqlQueryJson("select name from Users,Class where class.stuid = Users.userid and class.classid = '" + newclass.classid + "'");
            //方法2
            outStr = db.Queryable<Class>()
                .JoinTable<Users>((cl, us) => us.userid == cl.stuid)//两表共有的条件on.....
                .Where<Class>(cl => cl.classid == newclass.classid)//主表的条件where.....
                .Select("name,sex")
                .ToJson();

三表查询 查询班级名称,该班级的学生姓名和用户id

            outStr = db.Queryable<Class>()
                .JoinTable<Users>((cl, us) => cl.stuid == us.userid)
                .JoinTable<MyClass>((cl, mc) => cl.classid == mc.classid)
                .Where(cl => cl.classid == newclass.classid)
                .Select("name,sex,className")
                .ToJson();

批量插入

            string strList = "[{'classid':'2','stuid':'2'},{'classid':'2','stuid':'1'},{'classid':'2','stuid':'3'}]";
            List<Class> cl = JsonConvert.DeserializeObject<List<Class>>(strList);
            foreach (var item in cl)
            {
                try
                {
                    db.Insert(item);
                    outStr = "插入成功!";
                }
                catch (Exception ex)
                {
                    outStr = ex.Message;
                }

            }

查看学习Math课程的学生的数量和名字

            //方法1 但是稍显复杂
            outStr = db.Queryable<Class>()
               .JoinTable<Users>((cls, us) => cls.stuid == us.userid, JoinType.Inner)
               .JoinTable<MyClass>((cls, mc) => cls.classid == mc.classid && mc.classType == "Math", JoinType.Inner)
               .Select("name").ToJson();

            //方法2 也不简单
            outStr = db.Sqlable()
                .From("Class", "cl")
                .Join("Users", "us", "cl.stuid", "us.userid", JoinType.Inner)
                .Join("MyClass", "mc", "mc.classid", "cl.classid", JoinType.Inner)
                .Where("mc.classType = 'Math'")
                .SelectToJson("name");

            //方法3
            outStr = db.SqlQueryJson("SELECT name FROM users,class,myclass where users.userid = class.stuid and class.classid = myclass.classid and classType= 'Math'");

分页查询

            //这里我创建了一个Page类来接收页面的Index和Size
  
       var pageIndex = pages.pageIndex; var pageSize = pages.pageSize; var totalCount = 0; var page = db.Queryable<Users>() .OrderBy(it => it.userid) .ToPageList(pageIndex, pageSize, ref totalCount); int totalpages = totalCount + 1 / pageSize;//保证最后一面即使不满足pagesize也占一面 pages.totalPages = totalpages;
       //最后你可以处理成Json返回你要的数据

简单去重

            outStr = db.Queryable<Users>()
            .Select("username,password").GroupBy("username,password").ToJson();
            // 性能优于distinct