SugarSql的基本使用方法(1)

☆樱花仙子☆ 提交于 2020-05-04 13:24:19
using SqlSugar;
using SqlSugar.Helper;
using SqlSugar.Model;
using System;
using System.Collections.Generic;
using System.Linq;

namespace SugarSql
{
    class Program
    {
        
        static void Main(string[] args)
        {
            SqlSugarClient db = new SqlHelper().CreateDB();
            #region 新增
            //int changeNum = db.Insertable(new User() { Name = "张三", Address = "NJ", Gender = "男", Birth = DateTime.Today }).ExecuteCommand();
            //int addid = db.Insertable(new User() { Name = "李四", Address = "BJ", Gender = "女", Birth = DateTime.Today }).ExecuteReturnIdentity();
            //List<User> users = new List<User>()
            //{
            //    new User() { Name = "Jerry", Address = "NJ", Gender = "男", Birth = DateTime.Today },
            //    new User() { Name = "Nacy", Address = "BJ", Gender = "女", Birth = DateTime.Today }
            //};
            //int nums = db.Insertable(users).ExecuteCommand();
            #endregion

            #region 删除
            //db.Deleteable<User>(1).ExecuteCommand();
            //db.Deleteable<User>(u=>u.ID == 2).ExecuteCommand();
            //db.Deleteable<User>(new int[] { 3, 4, 5 }).ExecuteCommand();
            #endregion

            #region 更新
            //db.Updateable(new User() {ID = 6, Name = "Jerry", Address = "NJ", Gender = "男", Birth = DateTime.Today }).ExecuteCommand();
            //db.Updateable(new User() {Name = "Jerry", Address = "NJ", Gender = "男", Birth = DateTime.Today }).Where(it => it.Name == "Jerry").ExecuteCommand();
            #endregion

            # region 查询
            var users = db.Queryable<User>().ToList();
            var top2users = db.Queryable<User>().Take(2).ToList();
            var user = db.Queryable<User>().InSingle(2);
            //var getSingleOrDefault = db.Queryable<User>().Single();
            var getFirstOrDefault = db.Queryable<User>().First();

            int[] array = new int[] { 1, 2 };
            db.Queryable<User>().Where(it => array.Contains(it.ID)).ToList();
            db.Queryable<User>().Where(it => !array.Contains(it.ID)).ToList();

            db.Queryable<User>().Where(it => it.ID == 1 || it.Name == "张三").ToList();
            db.Queryable<User>().Where(it => SqlFunc.IsNullOrEmpty(it.Name)).ToList();

            string name = "zhangsan";
            db.Queryable<User>().WhereIF(!string.IsNullOrEmpty(name),it=>it.Name== name).ToList();
            # endregion
        }
    }
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!