From ebea347c580108acbb52336c9288f8a2a4e110ac Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Mon, 6 Nov 2023 10:31:08 +0800 Subject: [PATCH] Update demo --- Src/Asp.Net/SqlServerTest/1_CodeFirst.cs | 30 ++++ Src/Asp.Net/SqlServerTest/3_EasyQuery.cs | 137 ++++++++++++++++++ Src/Asp.Net/SqlServerTest/Program.cs | 1 + .../SqlServerTest/SqlServerTest.csproj | 1 + 4 files changed, 169 insertions(+) create mode 100644 Src/Asp.Net/SqlServerTest/3_EasyQuery.cs diff --git a/Src/Asp.Net/SqlServerTest/1_CodeFirst.cs b/Src/Asp.Net/SqlServerTest/1_CodeFirst.cs index ad5e6eaba..8d7bfdbb5 100644 --- a/Src/Asp.Net/SqlServerTest/1_CodeFirst.cs +++ b/Src/Asp.Net/SqlServerTest/1_CodeFirst.cs @@ -1,6 +1,7 @@ using SqlSugar; using System; using System.Collections.Generic; +using System.Data.Linq; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -28,6 +29,10 @@ namespace OrmTest // 根据 UserInfo001 实体类初始化表 db.CodeFirst.InitTables(); + //Table structure and class are different + //表结构和类存在差异 初始化表 + db.CodeFirst.InitTables(); + //Insert //插入 var id=db.Insertable(new UserInfo001() @@ -101,5 +106,30 @@ namespace OrmTest [SugarColumn(IsNullable = true)] public DateTime? RegistrationDate { get; set; } } + + + /// + /// User information entity class + /// 用户信息实体类 + /// + [SugarTable("UserInfoA")] + public class UserInfo002 + { + /// + /// User ID (Primary Key) + /// 用户ID(主键) + /// + [SugarColumn(IsIdentity = true,ColumnName ="Id", IsPrimaryKey = true)] + public int UserId { get; set; } + + /// + /// User name + /// 用户名 + /// + [SugarColumn(Length = 50,ColumnName ="Name", IsNullable = false)] + public string UserName { get; set; } + + + } } } \ No newline at end of file diff --git a/Src/Asp.Net/SqlServerTest/3_EasyQuery.cs b/Src/Asp.Net/SqlServerTest/3_EasyQuery.cs new file mode 100644 index 000000000..ac35fca2c --- /dev/null +++ b/Src/Asp.Net/SqlServerTest/3_EasyQuery.cs @@ -0,0 +1,137 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using SqlSugar; +namespace OrmTest +{ + internal class _3_EasyQuery + { + + + public static void Init() + { + + CreateTable(); + GetAllStudents(); + GetStudentCount(); + GetStudentsByCondition(); + GetStudentsByName("jack"); + GetStudentById(1); + GetMaxStudentId(); + GetStudentsOrderedByIdDesc(); + GetStudentNames(); + } + + private static void CreateTable() + { + SqlSugarClient db = DbHelper.GetNewDb(); + db.CodeFirst.InitTables(); + db.Insertable(new Student03() { Name = "name" + SnowFlakeSingle.Instance.NextId() }) + .ExecuteCommand(); + } + + // 查询所有学生信息 + // Query all student records + public static List GetAllStudents() + { + SqlSugarClient db = DbHelper.GetNewDb(); + return db.Queryable().ToList(); + } + + // 查询学生总数 + // Get the total count of students + public static int GetStudentCount() + { + SqlSugarClient db = DbHelper.GetNewDb(); + return db.Queryable().Count(); + } + + // 按条件查询学生信息 + // Query student records based on conditions + public static List GetStudentsByCondition() + { + SqlSugarClient db = DbHelper.GetNewDb(); + + // 查询Id为1的学生 + // Query students with Id equal to 1 + var studentsWithId1 = db.Queryable().Where(it => it.Id == 1).ToList(); + + // 查询name字段不为null的学生 + // Query students where the 'name' field is not null + var studentsWithNameNotNull = db.Queryable().Where(it => it.Name != null).ToList(); + + // 查询name字段为null的学生 + // Query students where the 'name' field is null + var studentsWithNameNull = db.Queryable().Where(it => it.Name == null).ToList(); + + // 查询name字段不为空的学生 + // Query students where the 'name' field is not empty + var studentsWithNameNotEmpty = db.Queryable().Where(it => it.Name != "").ToList(); + + // 多条件查询 + // Query students with multiple conditions + var studentsWithMultipleConditions = db.Queryable().Where(it => it.Id > 10 && it.Name == "a").ToList(); + + // 动态OR查询 + // Dynamic OR query + var exp = Expressionable.Create(); + exp.OrIF(true, it => it.Id == 1); + exp.Or(it => it.Name.Contains("jack")); + var studentsWithDynamicOr = db.Queryable().Where(exp.ToExpression()).ToList(); + + return studentsWithDynamicOr; + } + + // 模糊查询 + // Fuzzy search + public static List GetStudentsByName(string keyword) + { + SqlSugarClient db = DbHelper.GetNewDb(); + return db.Queryable().Where(it => it.Name.Contains(keyword)).ToList(); + } + + // 根据主键查询单个学生 + // Query a single student by primary key + public static Student03 GetStudentById(int id) + { + SqlSugarClient db = DbHelper.GetNewDb(); + return db.Queryable().Single(it => it.Id == id); + } + + // 获取订单表中的最大Id + // Get the maximum Id from the Student03 table + public static int GetMaxStudentId() + { + SqlSugarClient db = DbHelper.GetNewDb(); + return db.Queryable().Max(it => it.Id); + } + + // 简单排序 + // Simple sorting + public static List GetStudentsOrderedByIdDesc() + { + SqlSugarClient db = DbHelper.GetNewDb(); + return db.Queryable().OrderBy(sc => sc.Id, OrderByType.Desc).ToList(); + } + + // 查询一列 + // Query a single column + public static List GetStudentNames() + { + SqlSugarClient db = DbHelper.GetNewDb(); + return db.Queryable().Select(it => it.Name).ToList(); + } + + + + public class Student03 + { + [SugarColumn(IsPrimaryKey =true,IsIdentity =true)] + public int Id { get; set; } + public string Name { get; set; } + } + + } +} diff --git a/Src/Asp.Net/SqlServerTest/Program.cs b/Src/Asp.Net/SqlServerTest/Program.cs index ab122013b..b7808ac34 100644 --- a/Src/Asp.Net/SqlServerTest/Program.cs +++ b/Src/Asp.Net/SqlServerTest/Program.cs @@ -10,6 +10,7 @@ namespace OrmTest { _1_CodeFirst.Init(); _2_DbFirst.Init(); + _3_EasyQuery.Init(); Console.ReadKey(); } diff --git a/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj b/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj index 8cc71d44c..5eef7ceda 100644 --- a/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj +++ b/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj @@ -67,6 +67,7 @@ +