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
}
}
}
来源:oschina
链接:https://my.oschina.net/stupidpotato/blog/4264251