mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-04-05 17:37:58 +08:00
Update demo
This commit is contained in:
parent
777a7c6ddc
commit
ebea347c58
@ -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<UserInfo001>();
|
||||
|
||||
//Table structure and class are different
|
||||
//表结构和类存在差异 初始化表
|
||||
db.CodeFirst.InitTables<UserInfo002>();
|
||||
|
||||
//Insert
|
||||
//插入
|
||||
var id=db.Insertable(new UserInfo001()
|
||||
@ -101,5 +106,30 @@ namespace OrmTest
|
||||
[SugarColumn(IsNullable = true)]
|
||||
public DateTime? RegistrationDate { get; set; }
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// User information entity class
|
||||
/// 用户信息实体类
|
||||
/// </summary>
|
||||
[SugarTable("UserInfoA")]
|
||||
public class UserInfo002
|
||||
{
|
||||
/// <summary>
|
||||
/// User ID (Primary Key)
|
||||
/// 用户ID(主键)
|
||||
/// </summary>
|
||||
[SugarColumn(IsIdentity = true,ColumnName ="Id", IsPrimaryKey = true)]
|
||||
public int UserId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// User name
|
||||
/// 用户名
|
||||
/// </summary>
|
||||
[SugarColumn(Length = 50,ColumnName ="Name", IsNullable = false)]
|
||||
public string UserName { get; set; }
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
137
Src/Asp.Net/SqlServerTest/3_EasyQuery.cs
Normal file
137
Src/Asp.Net/SqlServerTest/3_EasyQuery.cs
Normal file
@ -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<Student03>();
|
||||
db.Insertable(new Student03() { Name = "name" + SnowFlakeSingle.Instance.NextId() })
|
||||
.ExecuteCommand();
|
||||
}
|
||||
|
||||
// 查询所有学生信息
|
||||
// Query all student records
|
||||
public static List<Student03> GetAllStudents()
|
||||
{
|
||||
SqlSugarClient db = DbHelper.GetNewDb();
|
||||
return db.Queryable<Student03>().ToList();
|
||||
}
|
||||
|
||||
// 查询学生总数
|
||||
// Get the total count of students
|
||||
public static int GetStudentCount()
|
||||
{
|
||||
SqlSugarClient db = DbHelper.GetNewDb();
|
||||
return db.Queryable<Student03>().Count();
|
||||
}
|
||||
|
||||
// 按条件查询学生信息
|
||||
// Query student records based on conditions
|
||||
public static List<Student03> GetStudentsByCondition()
|
||||
{
|
||||
SqlSugarClient db = DbHelper.GetNewDb();
|
||||
|
||||
// 查询Id为1的学生
|
||||
// Query students with Id equal to 1
|
||||
var studentsWithId1 = db.Queryable<Student03>().Where(it => it.Id == 1).ToList();
|
||||
|
||||
// 查询name字段不为null的学生
|
||||
// Query students where the 'name' field is not null
|
||||
var studentsWithNameNotNull = db.Queryable<Student03>().Where(it => it.Name != null).ToList();
|
||||
|
||||
// 查询name字段为null的学生
|
||||
// Query students where the 'name' field is null
|
||||
var studentsWithNameNull = db.Queryable<Student03>().Where(it => it.Name == null).ToList();
|
||||
|
||||
// 查询name字段不为空的学生
|
||||
// Query students where the 'name' field is not empty
|
||||
var studentsWithNameNotEmpty = db.Queryable<Student03>().Where(it => it.Name != "").ToList();
|
||||
|
||||
// 多条件查询
|
||||
// Query students with multiple conditions
|
||||
var studentsWithMultipleConditions = db.Queryable<Student03>().Where(it => it.Id > 10 && it.Name == "a").ToList();
|
||||
|
||||
// 动态OR查询
|
||||
// Dynamic OR query
|
||||
var exp = Expressionable.Create<Student03>();
|
||||
exp.OrIF(true, it => it.Id == 1);
|
||||
exp.Or(it => it.Name.Contains("jack"));
|
||||
var studentsWithDynamicOr = db.Queryable<Student03>().Where(exp.ToExpression()).ToList();
|
||||
|
||||
return studentsWithDynamicOr;
|
||||
}
|
||||
|
||||
// 模糊查询
|
||||
// Fuzzy search
|
||||
public static List<Student03> GetStudentsByName(string keyword)
|
||||
{
|
||||
SqlSugarClient db = DbHelper.GetNewDb();
|
||||
return db.Queryable<Student03>().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<Student03>().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<Student03>().Max(it => it.Id);
|
||||
}
|
||||
|
||||
// 简单排序
|
||||
// Simple sorting
|
||||
public static List<Student03> GetStudentsOrderedByIdDesc()
|
||||
{
|
||||
SqlSugarClient db = DbHelper.GetNewDb();
|
||||
return db.Queryable<Student03>().OrderBy(sc => sc.Id, OrderByType.Desc).ToList();
|
||||
}
|
||||
|
||||
// 查询一列
|
||||
// Query a single column
|
||||
public static List<string> GetStudentNames()
|
||||
{
|
||||
SqlSugarClient db = DbHelper.GetNewDb();
|
||||
return db.Queryable<Student03>().Select(it => it.Name).ToList();
|
||||
}
|
||||
|
||||
|
||||
|
||||
public class Student03
|
||||
{
|
||||
[SugarColumn(IsPrimaryKey =true,IsIdentity =true)]
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -10,6 +10,7 @@ namespace OrmTest
|
||||
{
|
||||
_1_CodeFirst.Init();
|
||||
_2_DbFirst.Init();
|
||||
_3_EasyQuery.Init();
|
||||
|
||||
Console.ReadKey();
|
||||
}
|
||||
|
@ -67,6 +67,7 @@
|
||||
<Compile Include="1_CodeFirst.cs" />
|
||||
<Compile Include="2_DbFirst.cs" />
|
||||
<Compile Include="UnitTest\Description.cs" />
|
||||
<Compile Include="3_EasyQuery.cs" />
|
||||
<Compile Include="UserTestCases\Cases\OldDemo\Demo2_Updateable.cs" />
|
||||
<Compile Include="UserTestCases\Cases\OldDemo\Demo3_Insertable.cs" />
|
||||
<Compile Include="UserTestCases\Cases\OldDemo\Demo4_Deleteable.cs" />
|
||||
|
Loading…
Reference in New Issue
Block a user