mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-04-05 17:37:58 +08:00
修改DEMO
This commit is contained in:
parent
fc784b3a71
commit
aee611b8a8
@ -61,7 +61,7 @@ namespace OrmTest
|
||||
public static List<ViewOrder2> SyntaxSugar3()
|
||||
{
|
||||
var db = DbHelper.GetNewDb();
|
||||
var list = db.Queryable<Order, OrderItem, Custom>((o, i, c) => o.Id == i.OrderId && c.Id == o.CustomId)
|
||||
var list = db.Queryable<Order, OrderDetail, Custom>((o, i, c) => o.Id == i.OrderId && c.Id == o.CustomId)
|
||||
.Select((o, i, c) => new ViewOrder2 { Id = o.Id, Name = o.Name, CustomName = c.Name })
|
||||
.ToList();
|
||||
|
||||
|
112
Src/Asp.Net/SqlServerTest/5_PageQuery.cs
Normal file
112
Src/Asp.Net/SqlServerTest/5_PageQuery.cs
Normal file
@ -0,0 +1,112 @@
|
||||
using OrmTest;
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
public class _5_PageQuery
|
||||
{
|
||||
public static void Init()
|
||||
{
|
||||
|
||||
int pagenumber = 1;
|
||||
int pageSize = 20;
|
||||
int totalCount = 0;
|
||||
|
||||
SqlSugarClient db = DbHelper.GetNewDb();
|
||||
|
||||
//建表
|
||||
//Create table
|
||||
AddTestData(db);
|
||||
|
||||
// 同步分页方法
|
||||
// Synchronous pagination method
|
||||
SyncPagination(db, pagenumber, pageSize, ref totalCount);
|
||||
|
||||
// 异步分页方法
|
||||
// Asynchronous pagination method
|
||||
AsyncPagination(db, pagenumber, pageSize) .GetAwaiter() .GetResult();
|
||||
}
|
||||
|
||||
public static void AddTestData(SqlSugarClient db)
|
||||
{
|
||||
|
||||
//建表
|
||||
//Create table
|
||||
db.CodeFirst.InitTables<School, Student>();
|
||||
|
||||
// 添加学校数据
|
||||
// Add school data
|
||||
var school1 = new School { Name = "School A" };
|
||||
var school2 = new School { Name = "School B" };
|
||||
db.Insertable(school1).ExecuteCommand();
|
||||
db.Insertable(school2).ExecuteCommand();
|
||||
|
||||
// 添加学生数据
|
||||
// Add student data
|
||||
var student1 = new Student { SchoolId = school1.Id, Name = "John", CreateTime = DateTime.Now };
|
||||
var student2 = new Student { SchoolId = school1.Id, Name = "Alice", CreateTime = DateTime.Now };
|
||||
|
||||
db.Insertable(student1).ExecuteCommand();
|
||||
db.Insertable(student2).ExecuteCommand();
|
||||
|
||||
Console.WriteLine("Test data added successfully.");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 同步分页示例
|
||||
/// Synchronous pagination example
|
||||
/// </summary>
|
||||
/// <param name="db">数据库连接对象 Database connection object</param>
|
||||
/// <param name="pagenumber">页码 Page number</param>
|
||||
/// <param name="pageSize">每页大小 Page size</param>
|
||||
/// <param name="totalCount">总记录数 Total record count</param>
|
||||
public static void SyncPagination(SqlSugarClient db, int pagenumber, int pageSize, ref int totalCount)
|
||||
{
|
||||
// 同步单表分页
|
||||
// Synchronous pagination for a single table
|
||||
var page = db.Queryable<Student>().ToPageList(pagenumber, pageSize, ref totalCount);
|
||||
|
||||
// 同步多表分页
|
||||
// Synchronous pagination for multiple tables
|
||||
var list = db.Queryable<Student>().LeftJoin<School>((st, sc) => st.SchoolId == sc.Id)
|
||||
.Select((st, sc) => new { Id = st.Id, Name = st.Name, SchoolName = sc.Name })
|
||||
.ToPageList(pagenumber, pageSize, ref totalCount);
|
||||
|
||||
// offset分页
|
||||
// offset pagination
|
||||
var sqlServerPage = db.Queryable<Student>().ToOffsetPage(pagenumber, pageSize);
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步分页示例
|
||||
/// Asynchronous pagination example
|
||||
/// </summary>
|
||||
/// <param name="db">数据库连接对象 Database connection object</param>
|
||||
/// <param name="pagenumber">页码 Page number</param>
|
||||
/// <param name="pageSize">每页大小 Page size</param>
|
||||
public static async Task AsyncPagination(SqlSugarClient db, int pagenumber, int pageSize)
|
||||
{
|
||||
RefAsync<int> total = 0;
|
||||
// 异步分页
|
||||
// Asynchronous pagination
|
||||
var orders = await db.Queryable<Student>().ToPageListAsync(pagenumber, pageSize, total);
|
||||
}
|
||||
[SugarTable("Student05")]
|
||||
public class Student
|
||||
{
|
||||
[SugarColumn(IsIdentity =true,IsPrimaryKey =true)]
|
||||
public int Id { get; set; }
|
||||
public int SchoolId { get; set; }
|
||||
public string Name { get; set; }
|
||||
public DateTime CreateTime { get; set; }
|
||||
}
|
||||
[SugarTable("School05")]
|
||||
public class School
|
||||
{
|
||||
[SugarColumn(IsIdentity = true, IsPrimaryKey = true)]
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
}
|
||||
}
|
@ -12,6 +12,7 @@ namespace OrmTest
|
||||
_2_DbFirst.Init();
|
||||
_3_EasyQuery.Init();
|
||||
_4_JoinQuery.Init();
|
||||
_5_PageQuery.Init();
|
||||
Console.ReadKey();
|
||||
}
|
||||
}
|
||||
|
@ -67,6 +67,7 @@
|
||||
<Compile Include="1_CodeFirst.cs" />
|
||||
<Compile Include="2_DbFirst.cs" />
|
||||
<Compile Include="4_JoinQuery.cs" />
|
||||
<Compile Include="5_PageQuery.cs" />
|
||||
<Compile Include="UnitTest\Description.cs" />
|
||||
<Compile Include="3_EasyQuery.cs" />
|
||||
<Compile Include="UserTestCases\Cases\OldDemo\Demo2_Updateable.cs" />
|
||||
|
Loading…
Reference in New Issue
Block a user