From af2166ba906a91f708b8448b0f674424d067d589 Mon Sep 17 00:00:00 2001
From: sunkaixuan <610262374@qq.com>
Date: Mon, 6 Nov 2023 10:54:26 +0800
Subject: [PATCH] Update demo
---
Src/Asp.Net/SqlServerTest/3_EasyQuery.cs | 148 ++++++++++++++++-------
1 file changed, 101 insertions(+), 47 deletions(-)
diff --git a/Src/Asp.Net/SqlServerTest/3_EasyQuery.cs b/Src/Asp.Net/SqlServerTest/3_EasyQuery.cs
index ac35fca2c..b4544bab0 100644
--- a/Src/Asp.Net/SqlServerTest/3_EasyQuery.cs
+++ b/Src/Asp.Net/SqlServerTest/3_EasyQuery.cs
@@ -4,26 +4,58 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SqlSugar;
-namespace OrmTest
+
+namespace OrmTest
{
internal class _3_EasyQuery
{
-
-
- public static void Init()
+ ///
+ /// 初始化方法,包含各种查询操作的演示
+ /// Initialization method containing demonstrations of various query operations
+ ///
+ public static void Init()
{
-
+ // 创建表并插入一条记录
+ // Create table and insert a record
CreateTable();
+
+ // 查询所有学生信息
+ // Query all student records
GetAllStudents();
+
+ // 查询学生总数
+ // Get the total count of students
GetStudentCount();
+
+ // 按条件查询学生信息
+ // Query student records based on conditions
GetStudentsByCondition();
+
+ // 模糊查询学生信息(名字包含"jack"的学生)
+ // Fuzzy search for student records (students with names containing "jack")
GetStudentsByName("jack");
+
+ // 根据学生ID查询单个学生
+ // Query a single student by student ID
GetStudentById(1);
+
+ // 获取Student03表中的最大Id
+ // Get the maximum ID from the Student03 table
GetMaxStudentId();
+
+ // 简单排序(按照Id降序排序)
+ // Simple sorting (sorting by Id in descending order)
GetStudentsOrderedByIdDesc();
+
+ // 查询学生姓名列表
+ // Query the list of student names
GetStudentNames();
}
+ ///
+ /// 创建表并插入一条记录
+ /// Create table and insert a record
+ ///
private static void CreateTable()
{
SqlSugarClient db = DbHelper.GetNewDb();
@@ -32,32 +64,38 @@ namespace OrmTest
.ExecuteCommand();
}
- // 查询所有学生信息
- // Query all student records
- public static List GetAllStudents()
+ ///
+ /// 查询所有学生信息
+ /// Query all student records
+ ///
+ private static void GetAllStudents()
{
SqlSugarClient db = DbHelper.GetNewDb();
- return db.Queryable().ToList();
+ var students = db.Queryable().ToList();
+ // 处理查询结果
+ // Process the query results
}
- // 查询学生总数
- // Get the total count of students
- public static int GetStudentCount()
+ ///
+ /// 查询学生总数
+ /// Get the total count of students
+ ///
+ private static void GetStudentCount()
{
SqlSugarClient db = DbHelper.GetNewDb();
- return db.Queryable().Count();
+ var count = db.Queryable().Count();
+ // 处理查询结果
+ // Process the query results
}
- // 按条件查询学生信息
- // Query student records based on conditions
- public static List GetStudentsByCondition()
+ ///
+ /// 按条件查询学生信息
+ /// Query student records based on conditions
+ ///
+ private static void 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();
@@ -80,58 +118,74 @@ namespace OrmTest
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)
+ ///
+ /// 模糊查询学生信息
+ /// Fuzzy search for student records
+ ///
+ private static void GetStudentsByName(string keyword)
{
SqlSugarClient db = DbHelper.GetNewDb();
- return db.Queryable().Where(it => it.Name.Contains(keyword)).ToList();
+ var students = db.Queryable().Where(it => it.Name.Contains(keyword)).ToList();
+ // 处理查询结果
+ // Process the query results
}
- // 根据主键查询单个学生
- // Query a single student by primary key
- public static Student03 GetStudentById(int id)
+ ///
+ /// 根据学生ID查询单个学生
+ /// Query a single student by student ID
+ ///
+ private static void GetStudentById(int id)
{
SqlSugarClient db = DbHelper.GetNewDb();
- return db.Queryable().Single(it => it.Id == id);
+ var student = db.Queryable().Single(it => it.Id == id);
+ // 处理查询结果
+ // Process the query results
}
- // 获取订单表中的最大Id
- // Get the maximum Id from the Student03 table
- public static int GetMaxStudentId()
+ ///
+ /// 获取Student03表中的最大Id
+ /// Get the maximum ID from the Student03 table
+ ///
+ private static void GetMaxStudentId()
{
SqlSugarClient db = DbHelper.GetNewDb();
- return db.Queryable().Max(it => it.Id);
+ var maxId = db.Queryable().Max(it => it.Id);
+ // 处理查询结果
+ // Process the query results
}
- // 简单排序
- // Simple sorting
- public static List GetStudentsOrderedByIdDesc()
+ ///
+ /// 简单排序(按照Id降序排序)
+ /// Simple sorting (sorting by Id in descending order)
+ ///
+ private static void GetStudentsOrderedByIdDesc()
{
SqlSugarClient db = DbHelper.GetNewDb();
- return db.Queryable().OrderBy(sc => sc.Id, OrderByType.Desc).ToList();
+ var students = db.Queryable().OrderBy(sc => sc.Id, OrderByType.Desc).ToList();
+ // 处理查询结果
+ // Process the query results
}
- // 查询一列
- // Query a single column
- public static List GetStudentNames()
+ ///
+ /// 查询学生姓名列表
+ /// Query the list of student names
+ ///
+ private static void GetStudentNames()
{
SqlSugarClient db = DbHelper.GetNewDb();
- return db.Queryable().Select(it => it.Name).ToList();
+ var studentNames = db.Queryable().Select(it => it.Name).ToList();
+ // 处理查询结果
+ // Process the query results
}
-
-
public class Student03
{
- [SugarColumn(IsPrimaryKey =true,IsIdentity =true)]
+ [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
public int Id { get; set; }
public string Name { get; set; }
}
-
}
-}
+}
\ No newline at end of file