diff --git a/Src/Asp.NetCore2/SqliteTest/1_CodeFirst.cs b/Src/Asp.NetCore2/SqliteTest/1_CodeFirst.cs new file mode 100644 index 000000000..14f37a858 --- /dev/null +++ b/Src/Asp.NetCore2/SqliteTest/1_CodeFirst.cs @@ -0,0 +1,134 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + /// + /// Class for demonstrating CodeFirst initialization operations + /// 用于展示 CodeFirst 初始化操作的类 + /// + public class _1_CodeFirst + { + + public static void Init() + { + // Get a new database instance + // 获取新的数据库实例 + var db = DbHelper.GetNewDb(); + + // Create the database if it doesn't exist + // 如果数据库不存在,则创建数据库 + db.DbMaintenance.CreateDatabase(); + + // Initialize tables based on UserInfo001 entity class + // 根据 UserInfo001 实体类初始化表 + db.CodeFirst.InitTables(); + + //Table structure and class are different + //表结构和类存在差异 初始化表 + db.CodeFirst.InitTables(); + + //Insert + //插入 + var id=db.Insertable(new UserInfo001() + { + Context = "Context", + Email="dfafa@qq.com", + Price=Convert.ToDecimal(1.1), + UserName="admin", + RegistrationDate=DateTime.Now, + + }).ExecuteReturnIdentity(); + + //Query + //查询 + var userInfo=db.Queryable().InSingle(id); + + //Update + //更新 + db.Updateable(userInfo).ExecuteCommand(); + + //Delete + //删除 + db.Deleteable(userInfo).ExecuteCommand(); + } + + /// + /// User information entity class + /// 用户信息实体类 + /// + public class UserInfo001 + { + /// + /// User ID (Primary Key) + /// 用户ID(主键) + /// + [SugarColumn(IsIdentity = true, IsPrimaryKey = true)] + public int UserId { get; set; } + + /// + /// User name + /// 用户名 + /// + [SugarColumn(Length = 50, IsNullable = false)] + public string UserName { get; set; } + + /// + /// User email + /// 用户邮箱 + /// + [SugarColumn(IsNullable = true)] + public string Email { get; set; } + + + /// + /// Product price + /// 产品价格 + /// + public decimal Price { get; set; } + + /// + /// User context + /// 用户内容 + /// + [SugarColumn(ColumnDataType = StaticConfig.CodeFirst_BigString, IsNullable = true)] + public string Context { get; set; } + + /// + /// User registration date + /// 用户注册日期 + /// + [SugarColumn(IsNullable = true)] + public DateTime? RegistrationDate { get; set; } + } + + + /// + /// User information entity class + /// 用户信息实体类 + /// + [SugarTable("UserInfoAAA01")] + 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.NetCore2/SqliteTest/2_DbFirst.cs b/Src/Asp.NetCore2/SqliteTest/2_DbFirst.cs new file mode 100644 index 000000000..a4ba49fc0 --- /dev/null +++ b/Src/Asp.NetCore2/SqliteTest/2_DbFirst.cs @@ -0,0 +1,136 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + internal class _2_DbFirst + { + /// + /// 初始化方法,包含各种DbFirst操作的演示 + /// Initialization method containing demonstrations of various DbFirst operations + /// + public static void Init() + { + var db = DbHelper.GetNewDb(); + + // 生成干净的实体类文件 + // Generate clean entity class files + Demo1(db); + + // 生成带有SqlSugar特性的实体类文件 + // Generate entity class files with SqlSugar attributes + Demo2(db); + + // 支持字符串类型的Nullable特性 + // Support String Nullable attribute + Demo3(db); + + // 格式化类名、属性名和文件名 + // Format class names, property names, and file names + Demo4(db); + + // 条件过滤生成实体类文件 + // Generate entity class files with condition filtering + Demo5(db); + + // 修改模版生成实体类文件(禁用IsCreateAttribute,避免冲突) + // Generate entity class files with modified templates (disable IsCreateAttribute to avoid conflicts) + Demo6(db); + } + + /// + /// 生成干净的实体类文件 + /// Generate clean entity class files + /// + private static void Demo1(SqlSugarClient db) + { + db.DbFirst.CreateClassFile("c:\\Demo\\1", "Models"); + } + + /// + /// 生成带有SqlSugar特性的实体类文件 + /// Generate entity class files with SqlSugar attributes + /// + private static void Demo2(SqlSugarClient db) + { + db.DbFirst.IsCreateAttribute().CreateClassFile("c:\\Demo\\2", "Models"); + } + + /// + /// 支持字符串类型的Nullable特性 + /// Support String Nullable attribute + /// + private static void Demo3(SqlSugarClient db) + { + db.DbFirst.IsCreateAttribute().StringNullable().CreateClassFile("c:\\Demo\\3", "Models"); + } + + /// + /// 格式化类名、属性名和文件名 + /// Format class names, property names, and file names + /// + private static void Demo4(SqlSugarClient db) + { + db.DbFirst + .IsCreateAttribute() + .FormatFileName(it => "File_" + it) + .FormatClassName(it => "Class_" + it) + .FormatPropertyName(it => "Property_" + it) + .CreateClassFile("c:\\Demo\\4", "Models"); + } + + /// + /// 条件过滤生成实体类文件 + /// Generate entity class files with condition filtering + /// + private static void Demo5(SqlSugarClient db) + { + db.DbFirst.IsCreateAttribute().Where(it => it.ToLower() == "userinfo001").CreateClassFile("c:\\Demo\\5", "Models"); + } + + /// + /// 修改模版生成实体类文件(禁用IsCreateAttribute,避免冲突) + /// Generate entity class files with modified templates (disable IsCreateAttribute to avoid conflicts) + /// + private static void Demo6(SqlSugarClient db) + { + db.DbFirst + // 类 + .SettingClassTemplate(old => { return old;/* 修改old值替换 */ }) + // 类构造函数 + .SettingConstructorTemplate(old => { return old;/* 修改old值替换 */ }) + .SettingNamespaceTemplate(old => + { + return old + "\r\nusing SqlSugar;"; // 追加引用SqlSugar + }) + // 属性备注 + .SettingPropertyDescriptionTemplate(old => { return old;/* 修改old值替换 */}) + + // 属性:新重载 完全自定义用配置 + .SettingPropertyTemplate((columns, temp, type) => + { + + var columnattribute = "\r\n [SugarColumn({0})]"; + List attributes = new List(); + if (columns.IsPrimarykey) + attributes.Add("IsPrimaryKey=true"); + if (columns.IsIdentity) + attributes.Add("IsIdentity=true"); + if (attributes.Count == 0) + { + columnattribute = ""; + } + return temp.Replace("{PropertyType}", type) + .Replace("{PropertyName}", columns.DbColumnName) + .Replace("{SugarColumn}", string.Format(columnattribute, string.Join(",", attributes))); + }) + + .CreateClassFile("c:\\Demo\\6"); + } + } +} + \ No newline at end of file diff --git a/Src/Asp.NetCore2/SqliteTest/3_EasyQuery.cs b/Src/Asp.NetCore2/SqliteTest/3_EasyQuery.cs new file mode 100644 index 000000000..b4544bab0 --- /dev/null +++ b/Src/Asp.NetCore2/SqliteTest/3_EasyQuery.cs @@ -0,0 +1,191 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using SqlSugar; + +namespace OrmTest +{ + internal class _3_EasyQuery + { + /// + /// 初始化方法,包含各种查询操作的演示 + /// 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(); + db.CodeFirst.InitTables(); + db.Insertable(new Student03() { Name = "name" + SnowFlakeSingle.Instance.NextId() }) + .ExecuteCommand(); + } + + /// + /// 查询所有学生信息 + /// Query all student records + /// + private static void GetAllStudents() + { + SqlSugarClient db = DbHelper.GetNewDb(); + var students = db.Queryable().ToList(); + // 处理查询结果 + // Process the query results + } + + /// + /// 查询学生总数 + /// Get the total count of students + /// + private static void GetStudentCount() + { + SqlSugarClient db = DbHelper.GetNewDb(); + var count = db.Queryable().Count(); + // 处理查询结果 + // Process the query results + } + + /// + /// 按条件查询学生信息 + /// Query student records based on conditions + /// + private static void GetStudentsByCondition() + { + SqlSugarClient db = DbHelper.GetNewDb(); + + // 查询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(); + + } + + /// + /// 模糊查询学生信息 + /// Fuzzy search for student records + /// + private static void GetStudentsByName(string keyword) + { + SqlSugarClient db = DbHelper.GetNewDb(); + var students = db.Queryable().Where(it => it.Name.Contains(keyword)).ToList(); + // 处理查询结果 + // Process the query results + } + + /// + /// 根据学生ID查询单个学生 + /// Query a single student by student ID + /// + private static void GetStudentById(int id) + { + SqlSugarClient db = DbHelper.GetNewDb(); + var student = db.Queryable().Single(it => it.Id == id); + // 处理查询结果 + // Process the query results + } + + /// + /// 获取Student03表中的最大Id + /// Get the maximum ID from the Student03 table + /// + private static void GetMaxStudentId() + { + SqlSugarClient db = DbHelper.GetNewDb(); + var maxId = db.Queryable().Max(it => it.Id); + // 处理查询结果 + // Process the query results + } + + /// + /// 简单排序(按照Id降序排序) + /// Simple sorting (sorting by Id in descending order) + /// + private static void GetStudentsOrderedByIdDesc() + { + SqlSugarClient db = DbHelper.GetNewDb(); + var students = db.Queryable().OrderBy(sc => sc.Id, OrderByType.Desc).ToList(); + // 处理查询结果 + // Process the query results + } + + /// + /// 查询学生姓名列表 + /// Query the list of student names + /// + private static void GetStudentNames() + { + SqlSugarClient db = DbHelper.GetNewDb(); + var studentNames = db.Queryable().Select(it => it.Name).ToList(); + // 处理查询结果 + // Process the query results + } + + public class Student03 + { + [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] + public int Id { get; set; } + public string Name { get; set; } + } + } +} \ No newline at end of file diff --git a/Src/Asp.NetCore2/SqliteTest/4_JoinQuery.cs b/Src/Asp.NetCore2/SqliteTest/4_JoinQuery.cs new file mode 100644 index 000000000..979152df8 --- /dev/null +++ b/Src/Asp.NetCore2/SqliteTest/4_JoinQuery.cs @@ -0,0 +1,171 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using static OrmTest._4_JoinQuery; + +namespace OrmTest +{ + internal class _4_JoinQuery + { + public static void Init() + { + InitializeDatabase(); + SyntaxSugar1(); + SyntaxSugar2(); + SyntaxSugar3(); + } + /// + /// 使用语法糖1进行联表查询。 + /// Performs a Join query using Syntax Sugar 1. + /// + /// 查询结果列表。The list of query results. + public static List SyntaxSugar1() + { + var db = DbHelper.GetNewDb(); + var query = db.Queryable() + .LeftJoin((o, cus) => o.CustomId == cus.Id) + .LeftJoin((o, cus, oritem) => o.Id == oritem.OrderId) + .Where(o => o.Id == 1) + .Select((o, cus, oritem) => new ViewOrder2 { Id = o.Id,Name=o.Name, CustomName = cus.Name }) + .ToList(); + + return query; + } + + /// + /// 使用语法糖2进行联表查询。 + /// Performs a Join query using Syntax Sugar 2. + /// + /// 查询结果列表。The list of query results. + public static List SyntaxSugar2() + { + var db = DbHelper.GetNewDb(); + var rightQueryable = db.Queryable() + .LeftJoin((o, i) => o.Id == i.Id) + .Select(o => o); + + var list = db.Queryable() + .LeftJoin(rightQueryable, (c, j) => c.CustomId == j.Id) + .Select(c => c) + .ToList(); + return list; + } + + /// + /// 使用语法糖3进行联表查询。 + /// Performs a Join query using Syntax Sugar 3. + /// + /// 查询结果列表。The list of query results. + public static List SyntaxSugar3() + { + var db = DbHelper.GetNewDb(); + var list = db.Queryable((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(); + + return list; + } + + + static void InitializeDatabase() + { + // Initialize order data + // 初始化订单数据 + var orders = new List + { + new Order { Id = 1, Name = "Order 1", CustomId = 1 }, + new Order { Id = 2, Name = "Order 2", CustomId = 2 }, + new Order { Id = 3, Name = "Order 3", CustomId = 1 }, + }; + + // Initialize order details data + // 初始化订单详情数据 + var orderDetails = new List + { + new OrderDetail { Id = 1, OrderId = 1 }, + new OrderDetail { Id = 2, OrderId = 2 }, + new OrderDetail { Id = 3, OrderId = 3 }, + }; + + // Initialize customer data + // 初始化客户数据 + var customers = new List + { + new Custom { Id = 1, Name = "Customer 1" }, + new Custom { Id = 2, Name = "Customer 2" }, + new Custom { Id = 3, Name = "Customer 3" }, + }; + + // Get database connection + // 获取数据库连接 + var db = DbHelper.GetNewDb(); + + // Initialize database tables and truncate data + // 初始化数据库表并清空数据 + db.CodeFirst.InitTables(); + db.DbMaintenance.TruncateTable(); + + // Insert data into tables + // 向表中插入数据 + db.Insertable(orders).ExecuteCommand(); + db.Insertable(orderDetails).ExecuteCommand(); + db.Insertable(customers).ExecuteCommand(); + } + + /// + /// 订单实体类。 + /// Order entity class. + /// + [SqlSugar.SugarTable("Order04")] + public class Order + { + [SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true)] + public int Id { get; set; } + public string Name { get; set; } + public int CustomId { get; set; } + // 其他订单相关属性... + } + + /// + /// 订单详情实体类。 + /// Order detail entity class. + /// + [SqlSugar.SugarTable("OrderDetail04")] + public class OrderDetail + { + [SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true)] + public int Id { get; set; } + public int OrderId { get; set; } + // 其他订单详情相关属性... + } + + /// + /// 客户实体类。 + /// Customer entity class. + /// + [SqlSugar.SugarTable("Custom04")] + public class Custom + { + [SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true)] + public int Id { get; set; } + public string Name { get; set; } + // 其他客户相关属性... + } + + + + /// + /// 类1实体类。 + /// Class1 entity class. + /// + public class ViewOrder2 + { + public int Id { get; set; } + public string Name { get; set; } + public string CustomName { get; set; } + // 其他类1相关属性... + } + } +} diff --git a/Src/Asp.NetCore2/SqliteTest/5_PageQuery.cs b/Src/Asp.NetCore2/SqliteTest/5_PageQuery.cs new file mode 100644 index 000000000..fba71c6f7 --- /dev/null +++ b/Src/Asp.NetCore2/SqliteTest/5_PageQuery.cs @@ -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(); + + // 添加学校数据 + // 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."); + } + + /// + /// 同步分页示例 + /// Synchronous pagination example + /// + /// 数据库连接对象 Database connection object + /// 页码 Page number + /// 每页大小 Page size + /// 总记录数 Total record count + public static void SyncPagination(SqlSugarClient db, int pagenumber, int pageSize, ref int totalCount) + { + // 同步单表分页 + // Synchronous pagination for a single table + var page = db.Queryable().ToPageList(pagenumber, pageSize, ref totalCount); + + // 同步多表分页 + // Synchronous pagination for multiple tables + var list = db.Queryable().LeftJoin((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().ToOffsetPage(pagenumber, pageSize); + + } + + /// + /// 异步分页示例 + /// Asynchronous pagination example + /// + /// 数据库连接对象 Database connection object + /// 页码 Page number + /// 每页大小 Page size + public static async Task AsyncPagination(SqlSugarClient db, int pagenumber, int pageSize) + { + RefAsync total = 0; + // 异步分页 + // Asynchronous pagination + var orders = await db.Queryable().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; } + } +} \ No newline at end of file diff --git a/Src/Asp.NetCore2/SqliteTest/6_NavigationPropertyQuery.cs b/Src/Asp.NetCore2/SqliteTest/6_NavigationPropertyQuery.cs new file mode 100644 index 000000000..6dec5075e --- /dev/null +++ b/Src/Asp.NetCore2/SqliteTest/6_NavigationPropertyQuery.cs @@ -0,0 +1,268 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + internal class _6_NavQuery + { + /// + /// Initialize navigation query examples. + /// 初始化导航查询示例。 + /// + public static void Init() + { + var db = DbHelper.GetNewDb(); + + // Initialize database table structures. + // 初始化数据库表结构。 + InitializeDatabase(db); + + // One-to-One navigation query test. + // 一对一导航查询测试。 + OneToOneTest(db); + + // One-to-Many navigation query test. + // 一对多导航查询测试。 + OneToManyTest(db); + + // Many-to-Many navigation query test. + // 多对多导航查询测试。 + ManyToManyTest(db); + } + + /// + /// Test many-to-many navigation queries. + /// 测试多对多导航查询。 + /// + private static void ManyToManyTest(SqlSugarClient db) + { + // Many-to-many navigation query, query table A and include its BList. + // 多对多导航查询,查询A表,并包含其BList。 + var list4 = db.Queryable().Includes(it => it.BList).ToList(); + + // Many-to-many navigation query with filtered BList while preserving the original A records, regardless of the filter on BList. + // 使用过滤条件的多对多导航查询,在过滤BList的同时保持A表的原始记录,不受BList过滤条件的影响。 + var list5 = db.Queryable().Includes(it => it.BList.Where(s => s.BId == 1).ToList()).ToList(); + + // Many-to-many navigation query with filtered A records while preserving the original BList, regardless of the filter on A records. + // 使用过滤条件的多对多导航查询,在过滤A表的同时保持BList的原始记录,不受A表过滤条件的影响。 + var list6 = db.Queryable().Includes(it => it.BList) + .Where(it =>it.BList.Any(s => s.BId == 1)).ToList(); + + // Many-to-many navigation query with filtered BList and filtered A records, but not preserving the original A and B records. + // 使用过滤条件的多对多导航查询,在A表中过滤BList并过滤A记录,但不保持A表和B表的原始记录。 + var list7 = db.Queryable() + .Includes(it => it.BList.Where(s => s.BId == 1).ToList()) + .Where(it => it.BList.Any(s => s.BId == 1)).ToList(); + } + + /// + /// Test one-to-many navigation queries. + /// 测试一对多导航查询。 + /// + private static void OneToManyTest(SqlSugarClient db) + { + // One-to-many navigation query, query table Student and include its Books. + // 一对多导航查询,查询Student表,并包含其Books。 + var list4 = db.Queryable().Includes(it => it.Books).ToList(); + + // One-to-many navigation query with filtered Books while preserving the original Student records, regardless of the filter on Books. + // 使用过滤条件的一对多导航查询,在过滤Books的同时保持Student表的原始记录,不受Books过滤条件的影响。 + var list5 = db.Queryable().Includes(it => it.Books.Where(s => s.BookId == 1).ToList()).ToList(); + + // One-to-many navigation query with filtered Student records while preserving the original Books, regardless of the filter on Student records. + // 使用过滤条件的一对多导航查询,在过滤Student表的同时保持Books的原始记录,不受Student表过滤条件的影响。 + var list6 = db.Queryable().Includes(it => it.Books) + .Where(it => it.Books.Any(s => s.BookId == 1)).ToList(); + + // One-to-many navigation query with filtered Books and filtered Student records, but not preserving the original Student and Books records. + // 使用过滤条件的一对多导航查询,在Student表中过滤Books并过滤Student记录,但不保持Student表和Books表的原始记录。 + var list7 = db.Queryable() + .Includes(it => it.Books.Where(s => s.BookId == 1).ToList()) + .Where(it => it.Books.Any(s => s.BookId == 1)).ToList(); + } + + /// + /// Test one-to-one navigation queries. + /// 测试一对一导航查询。 + /// + private static void OneToOneTest(SqlSugarClient db) + { + // One-to-one navigation query with condition, query table Student and include its associated School with specific SchoolId. + // 带条件的一对一导航查询,查询Student表,并包含其关联的School表,条件为特定的SchoolId。 + var list = db.Queryable() + .Where(it => it.School.SchoolId == 1) + .ToList(); + + // Inner join navigation query, query table Student and include its associated School. + // 内连接导航查询,查询Student表,并包含其关联的School表。 + var list2 = db.Queryable().IncludeInnerJoin(it => it.School) + .ToList(); + + // Includes navigation query, query table Student and include its associated School. + // 包含导航查询,查询Student表,并包含其关联的School表。 + var list3 = db.Queryable().Includes(it => it.School).ToList(); + } + + /// + /// Initialize database tables and insert sample data for navigation query examples. + /// 初始化导航查询示例的数据库表并插入示例数据。 + /// + private static void InitializeDatabase(SqlSugarClient db) + { + // Initialize and truncate tables for Student, School, and Book entities. + // 初始化并清空Student、School和Book表。 + db.CodeFirst.InitTables(); + db.DbMaintenance.TruncateTable(); + + // Sample data for Student, School, and Book entities. + // Student、School和Book表的示例数据。 + var students = new List + { + new Student + { + Name = "Student 1", + SexCode = "M", + School = new School { SchoolName = "School 1" }, + Books = new List + { + new Book { Name = "Book 1" }, + new Book { Name = "Book 2" } + } + }, + new Student + { + Name = "Student 2", + SexCode = "F", + School = new School { SchoolName = "School 2" }, + Books = new List + { + new Book { Name = "Book 3" } + } + } + }; + + // Insert sample data for Student, School, and Book entities using navigation properties. + // 使用导航属性插入Student、School和Book表的示例数据。 + db.InsertNav(students) + .Include(it => it.School) + .Include(it => it.Books).ExecuteCommand(); + + // Initialize and truncate tables for A, B, and ABMapping entities. + // 初始化并清空A、B和ABMapping表。 + db.CodeFirst.InitTables(); + db.DbMaintenance.TruncateTable(); + + // Sample data for A, B, and ABMapping entities. + // A、B和ABMapping表的示例数据。 + List a1 = new List { new A() { Name = "A1" }, new A() { Name = "A2" } }; + B b1 = new B { Name = "B1" }; + B b2 = new B { Name = "B2" }; + a1[0].BList = new List { b1, b2 }; + + // Insert sample data for A, B, and ABMapping entities using navigation properties. + // 使用导航属性插入A、B和ABMapping表的示例数据。 + db.InsertNav(a1).Include(x => x.BList).ExecuteCommand(); + } + + /// + /// Student entity representing the Student table in the database. + /// 表示数据库中Student表的Student实体类。 + /// + [SugarTable("Student06")] + public class Student + { + [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] + public int StudentId { get; set; } + public string Name { get; set; } + public string SexCode { get; set; } + public int SchoolId { get; set; } + + // One-to-One navigation property to School entity. + // 与School实体的一对一导航属性。 + [Navigate(NavigateType.OneToOne, nameof(SchoolId))] + public School School { get; set; } + + // One-to-Many navigation property to Book entities. + // 与Book实体的一对多导航属性。 + [Navigate(NavigateType.OneToMany, nameof(Book.StudentId))] + public List Books { get; set; } + } + + /// + /// School entity representing the School table in the database. + /// 表示数据库中School表的School实体类。 + /// + [SugarTable("School06")] + public class School + { + [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] + public int SchoolId { get; set; } + public string SchoolName { get; set; } + } + + /// + /// Book entity representing the Book table in the database. + /// 表示数据库中Book表的Book实体类。 + /// + [SugarTable("Book06")] + public class Book + { + [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] + public int BookId { get; set; } + public string Name { get; set; } + public int StudentId { get; set; } + } + + /// + /// A entity representing the A table in the database for many-to-many relationship. + /// 表示多对多关系中数据库中A表的A实体类。 + /// + [SugarTable("A06")] + public class A + { + [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] + public int AId { get; set; } + public string Name { get; set; } + + // Many-to-Many navigation property to B entities using ABMapping table. + // 与B实体的多对多导航属性,使用ABMapping表。 + [Navigate(typeof(ABMapping), nameof(ABMapping.AId), nameof(ABMapping.BId))] + public List BList { get; set; } + } + + /// + /// B entity representing the B table in the database for many-to-many relationship. + /// 表示多对多关系中数据库中B表的B实体类。 + /// + [SugarTable("B06")] + public class B + { + [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] + public int BId { get; set; } + public string Name { get; set; } + + // Many-to-Many navigation property to A entities using ABMapping table. + // 与A实体的多对多导航属性,使用ABMapping表。 + [Navigate(typeof(ABMapping), nameof(ABMapping.BId), nameof(ABMapping.AId))] + public List AList { get; set; } + } + + /// + /// ABMapping entity representing the intermediate table for many-to-many relationship between A and B entities. + /// 表示A和B实体之间多对多关系的中间表的ABMapping实体类。 + /// + [SugarTable("ABMapping06")] + public class ABMapping + { + [SugarColumn(IsPrimaryKey = true)] + public int AId { get; set; } + [SugarColumn(IsPrimaryKey = true)] + public int BId { get; set; } + } + } +} \ No newline at end of file diff --git a/Src/Asp.NetCore2/SqliteTest/7_GroupQuery.cs b/Src/Asp.NetCore2/SqliteTest/7_GroupQuery.cs new file mode 100644 index 000000000..6eabe8d54 --- /dev/null +++ b/Src/Asp.NetCore2/SqliteTest/7_GroupQuery.cs @@ -0,0 +1,74 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + internal class _7_GroupQuery + { + public static void Init() + { + var db = DbHelper.GetNewDb(); + List students = new List + { + new Student { Id = 1, Name = "Alice", Age = 20 }, + new Student { Id = 2, Name = "Bob", Age = 22 }, + new Student { Id = 3, Name = "Alice", Age = 21 }, + new Student { Id = 4, Name = "Charlie", Age = 19 }, + new Student { Id = 5, Name = "Bob", Age = 20 } + }; + + // 初始化数据库表结构,如果表不存在则创建 (Initialize database table structure; create if not exists) + db.CodeFirst.InitTables(); + + // 清空指定表中的所有数据 (Truncate all data in the specified table) + db.DbMaintenance.TruncateTable(); + + //插入记录(Insert datas) + db.Insertable(students).ExecuteCommand(); + + // 分组查询示例 (Grouping Query Example) + var groupedStudents = db.Queryable() + .GroupBy(s => s.Name) + .Select(g => new + { + Name = g.Name, // 学生姓名 (Student Name) + Count = SqlFunc.AggregateCount(g.Id), // 学生数量 (Count of Students) + AverageAge = SqlFunc.AggregateAvg(g.Age), // 平均年龄 (Average Age) + MaxAge = SqlFunc.AggregateMax(g.Age), // 最大年龄 (Maximum Age) + MinAge = SqlFunc.AggregateMin(g.Age) // 最小年龄 (Minimum Age) + }) + .ToList(); + + + // 去重查询示例 (Distinct Query Example) + var distinctNames = students.Select(s => s.Name).Distinct().ToList(); + + + // 分组取第一条记录示例 (Group First Record Example) + var groupFirstRecord = db.Queryable() + .Select(g => new + { + index = SqlFunc.RowNumber(SqlFunc.Desc(g.Id), g.Name), + Id = g.Id, + Name = g.Name, + Age =g.Age + }) + .MergeTable() + .Where(it => it.index == 1) + .ToList(); + } + + [SqlSugar.SugarTable("Student07")] + public class Student + { + [SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true)] + public int Id { get; set; } // 学生ID (Student ID) + public string Name { get; set; } // 学生姓名 (Student Name) + public int Age { get; set; } // 学生年龄 (Student Age) + } + } +} diff --git a/Src/Asp.NetCore2/SqliteTest/8_Insert.cs b/Src/Asp.NetCore2/SqliteTest/8_Insert.cs new file mode 100644 index 000000000..591a435f9 --- /dev/null +++ b/Src/Asp.NetCore2/SqliteTest/8_Insert.cs @@ -0,0 +1,66 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Data.Common; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + internal class _8_Insert + { + public static void Init() + { + var db = DbHelper.GetNewDb(); + + // 初始化实体表格(Initialize entity tables) + db.CodeFirst.InitTables(); + + // Use Case 1: 返回插入行数(Return the number of inserted rows) + var rowCount = db.Insertable(new StudentWithIdentity() { Name = "name" }).ExecuteCommand(); + + // Use Case 2: 插入数据并返回自增列(Insert data and return the auto-incremented column) + var identity = db.Insertable(new StudentWithIdentity() { Name = "name2" }).ExecuteReturnIdentity(); + + // Use Case 3: 返回雪花ID(Return the snowflake ID) + var snowflakeId = db.Insertable(new StudentWithSnowflake() { Name = "name" }).ExecuteReturnSnowflakeId(); + + // Use Case 4: 强制设置表名别名(Forcefully set table name alias) + db.Insertable(new StudentWithIdentity() { Name = "name2" }).AS("StudentWithIdentity").ExecuteCommand(); + + // Use Case 5: 批量插入实体(非参数化插入)(Batch insert entities (non-parameterized)) + var list = db.Queryable().Take(2).ToList(); + db.Insertable(list).ExecuteCommand(); + db.Insertable(list).PageSize(1000).ExecuteCommand(); + + // Use Case 6: 参数化内部分页插入(Parameterized internal pagination insert) + db.Insertable(list).UseParameter().ExecuteCommand(); + + // Use Case 7: 大数据写入(示例代码,请根据实际情况调整)(Bulk data insertion - Example code, adjust based on actual scenario) + var listLong = new List() { + new StudentWithSnowflake() { Name = "name",Id=SnowFlakeSingle.Instance.NextId() }, + new StudentWithSnowflake() { Name = "name",Id=SnowFlakeSingle.Instance.NextId()} + }; + db.Fastest().BulkCopy(listLong); + } + + // 实体类:带自增主键(Entity class: With auto-increment primary key) + [SugarTable("StudentWithIdentity08")] + public class StudentWithIdentity + { + [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] + public int Id { get; set; } + public string Name { get; set; } + } + + // 实体类:带雪花主键(Entity class: With snowflake primary key) + [SugarTable("StudentWithSnowflake08")] + public class StudentWithSnowflake + { + [SugarColumn(IsPrimaryKey = true)] + public long Id { get; set; } + public string Name { get; set; } + } + } +} \ No newline at end of file diff --git a/Src/Asp.NetCore2/SqliteTest/9_Update.cs b/Src/Asp.NetCore2/SqliteTest/9_Update.cs new file mode 100644 index 000000000..2fdcc0fdf --- /dev/null +++ b/Src/Asp.NetCore2/SqliteTest/9_Update.cs @@ -0,0 +1,88 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using static OrmTest._8_Insert; + +namespace OrmTest +{ + internal class _9_Update + { + /// + /// 初始化更新方法(Initialize update methods) + /// + internal static void Init() + { + var db = DbHelper.GetNewDb(); + + // 初始化实体表格(Initialize entity tables) + db.CodeFirst.InitTables(); + + // 创建一个需要更新的实体对象(Create an entity object to be updated) + var updateObj = new StudentWithSnowflake() { Id = 1, Name = "order1" }; + + // 创建需要批量更新的实体对象列表(Create a list of entity objects to be updated in bulk) + var updateObjs = new List { + new StudentWithSnowflake() { Id = 11, Name = "order11", Date=DateTime.Now }, + new StudentWithSnowflake() { Id = 12, Name = "order12", Date=DateTime.Now } + }; + + /***************************根据实体更新 (Update based on entity)***************************/ + + // 更新单个实体对象(Update a single entity object) + var result = db.Updateable(updateObj).ExecuteCommand(); + + // 批量更新实体对象列表(Update a list of entity objects in bulk) + var result20 = db.Updateable(updateObjs).ExecuteCommand(); + var result21 = db.Updateable(updateObjs).PageSize(500).ExecuteCommand(); + + // 更新实体对象,忽略指定列(Update entity object, ignoring specific columns) + var result3 = db.Updateable(updateObj).IgnoreColumns(it => new { it.Remark }).ExecuteCommand(); + + // 更新实体对象的指定列(Update specific columns of the entity object) + var result4 = db.Updateable(updateObj).UpdateColumns(it => new { it.Name, Date = DateTime.Now }).ExecuteCommand(); + + // 如果没有主键,按照指定列更新实体对象(If there is no primary key, update entity object based on specified columns) + var result5 = db.Updateable(updateObj).WhereColumns(it => new { it.Id }).ExecuteCommand(); + + // 如果字段值为NULL,不进行更新(Do not update columns with NULL values) + var result6 = db.Updateable(updateObj).IgnoreNullColumns().ExecuteCommand(); + + // 忽略为NULL和默认值的列进行更新(Ignore columns with NULL and default values during update) + var result7 = db.Updateable(updateObj) + .IgnoreColumns(ignoreAllNullColumns: true, ignoreAllDefaultValue:true) + .ExecuteCommand(); + + // 使用最快的方式批量更新实体对象列表(Bulk update a list of entity objects using the fastest method) + var result8 = db.Fastest().BulkUpdate(updateObjs); + + /***************************表达式更新 (Expression Update)***************************/ + + // 使用表达式更新实体对象的指定列(Update specific columns of the entity object using expressions) + var result71 = db.Updateable() + .SetColumns(it => new StudentWithSnowflake() { Name = "a", Date = DateTime.Now }) + .Where(it => it.Id == 11) + .ExecuteCommand(); + + // 使用表达式更新实体对象的指定列(Update specific columns of the entity object using expressions) + var result81 = db.Updateable() + .SetColumns(it => it.Name == "Name" + "1") + .Where(it => it.Id == 1) + .ExecuteCommand(); + } + + // 实体类:带雪花主键(Entity class: With snowflake primary key) + [SugarTable("StudentWithSnowflake09")] + public class StudentWithSnowflake + { + [SugarColumn(IsPrimaryKey = true)] + public long Id { get; set; } + public string Name { get; set; } + public DateTime Date { get; set; } + [SugarColumn(IsNullable = true)] + public string Remark { get; set; } + } + } +} \ No newline at end of file diff --git a/Src/Asp.NetCore2/SqliteTest/Program.cs b/Src/Asp.NetCore2/SqliteTest/Program.cs new file mode 100644 index 000000000..49b0b99bd --- /dev/null +++ b/Src/Asp.NetCore2/SqliteTest/Program.cs @@ -0,0 +1,64 @@ +using SqlSugar; +using System; + +namespace OrmTest +{ + + public class Program + { + static void Main(string[] args) + { + _1_CodeFirst.Init(); + _2_DbFirst.Init(); + _3_EasyQuery.Init(); + _4_JoinQuery.Init(); + _5_PageQuery.Init(); + _6_NavQuery.Init(); + _7_GroupQuery.Init(); + _8_Insert.Init(); + _9_Update.Init(); + _a1_Delete.Init(); + _a2_Sql.Init(); + _a3_Merge.Init(); + } + } + + /// + /// Helper class for database operations + /// 数据库操作的辅助类 + /// + public class DbHelper + { + /// + /// Database connection string + /// 数据库连接字符串 + /// + public readonly static string Connection = "datasource=SqlSugar5Demo"; + + /// + /// Get a new SqlSugarClient instance with specific configurations + /// 获取具有特定配置的新 SqlSugarClient 实例 + /// + /// SqlSugarClient instance + public static SqlSugarClient GetNewDb() + { + var db = new SqlSugarClient(new ConnectionConfig() + { + IsAutoCloseConnection = true, + DbType = DbType.Sqlite, + ConnectionString = Connection, + LanguageType=LanguageType.Default//Set language + + }, + it => { + // Logging SQL statements and parameters before execution + // 在执行前记录 SQL 语句和参数 + it.Aop.OnLogExecuting = (sql, para) => + { + Console.WriteLine(UtilMethods.GetNativeSql(sql, para)); + }; + }); + return db; + } + } +} \ No newline at end of file diff --git a/Src/Asp.NetCore2/SqliteTest/UnitTest/Program.cs b/Src/Asp.NetCore2/SqliteTest/UnitTest/Program.cs index b91106caf..de237db5a 100644 --- a/Src/Asp.NetCore2/SqliteTest/UnitTest/Program.cs +++ b/Src/Asp.NetCore2/SqliteTest/UnitTest/Program.cs @@ -3,9 +3,9 @@ using OrmTest; namespace SqliteTest.UnitTest { - class Program + public class cases { - static void Main(string[] args) + static void Init() { //Demo Demo0_SqlSugarClient.Init(); diff --git a/Src/Asp.NetCore2/SqliteTest/a1_Delete.cs b/Src/Asp.NetCore2/SqliteTest/a1_Delete.cs new file mode 100644 index 000000000..03b37ce43 --- /dev/null +++ b/Src/Asp.NetCore2/SqliteTest/a1_Delete.cs @@ -0,0 +1,186 @@ +using SqlSugar; +using System.Collections.Generic; + +namespace OrmTest +{ + internal class _a1_Delete + { + /// + /// 初始化删除操作的示例方法。 + /// Initializes example methods for delete operations. + /// + internal static void Init() + { + // 获取新的数据库连接对象 + // Get a new database connection object + var db = DbHelper.GetNewDb(); + + db.CodeFirst.InitTables(); + + // 调用各个删除操作的示例方法 + // Calling example methods for various delete operations + DeleteSingleEntity(db); + + // 批量删除实体的示例方法 + // Example method for deleting entities in batch + DeleteBatchEntities(db); + + // 批量删除并分页的示例方法 + // Example method for deleting entities in batch with paging + DeleteBatchEntitiesWithPaging(db); + + // 调用无主键实体删除的示例方法 + // Calling example method for deleting entities without primary key + DeleteEntitiesWithoutPrimaryKey(db); + + // 调用根据主键删除实体的示例方法 + // Calling example method for deleting entity by primary key + DeleteEntityByPrimaryKey(1, db); + + // 调用根据主键数组批量删除实体的示例方法 + // Calling example method for deleting entities by primary key array + DeleteEntitiesByPrimaryKeyArray(db); + + // 调用根据表达式删除实体的示例方法 + // Calling example method for deleting entities by expression + DeleteEntitiesByExpression(db); + + // 调用联表删除实体的示例方法 + // Calling example method for deleting entities with join + DeleteEntitiesWithJoin(db); + } + + /// + /// 删除单个实体的示例方法。 + /// Example method for deleting a single entity. + /// + internal static void DeleteSingleEntity(ISqlSugarClient db) + { + // 删除指定 Id 的学生实体 + // Delete the student entity with the specified Id + db.Deleteable(new Student() { Id = 1 }).ExecuteCommand(); + } + + /// + /// 批量删除实体的示例方法。 + /// Example method for deleting entities in batch. + /// + internal static void DeleteBatchEntities(ISqlSugarClient db) + { + // 创建学生实体列表 + // Create a list of student entities + List list = new List() + { + new Student() { Id = 1 } + }; + + // 批量删除学生实体 + // Delete student entities in batch + db.Deleteable(list).ExecuteCommand(); + } + + /// + /// 批量删除并分页的示例方法。 + /// Example method for deleting entities in batch with paging. + /// + internal static void DeleteBatchEntitiesWithPaging(ISqlSugarClient db) + { + // 创建订单实体列表 + // Create a list of order entities + List list = new List(); + + // 批量删除订单实体并分页 + // Delete order entities in batch with paging + db.Deleteable(list).PageSize(500).ExecuteCommand(); + } + + /// + /// 无主键实体删除的示例方法。 + /// Example method for deleting entities without primary key. + /// + internal static void DeleteEntitiesWithoutPrimaryKey( ISqlSugarClient db) + { + List orders = new List() + { + new Order() { Id = 1 }, + new Order() { Id = 2 } + }; + + // 根据指定的实体列表的 Id 列进行删除 + // Delete entities based on the Id column of the specified entity list + db.Deleteable().WhereColumns(orders, it => new { it.Id }).ExecuteCommand(); + } + + /// + /// 根据主键删除实体的示例方法。 + /// Example method for deleting an entity by primary key. + /// + internal static void DeleteEntityByPrimaryKey(int id, ISqlSugarClient db) + { + // 根据指定的 Id 删除学生实体 + // Delete the student entity with the specified Id + db.Deleteable().In(id).ExecuteCommand(); + } + + /// + /// 根据主键数组批量删除实体的示例方法。 + /// Example method for deleting entities by primary key array. + /// + internal static void DeleteEntitiesByPrimaryKeyArray(ISqlSugarClient db) + { + // 定义主键数组 + // Define an array of primary keys + int[] ids = { 1, 2 }; + + // 根据指定的 Id 数组批量删除学生实体 + // Delete student entities in batch based on the specified Id array + db.Deleteable().In(ids).ExecuteCommand(); + } + + /// + /// 根据表达式删除实体的示例方法。 + /// Example method for deleting entities by expression. + /// + internal static void DeleteEntitiesByExpression(ISqlSugarClient db) + { + // 根据指定的表达式删除学生实体 + // Delete the student entity based on the specified expression + db.Deleteable().Where(it => it.Id == 1).ExecuteCommand(); + } + + /// + /// 联表删除实体的示例方法。 + /// Example method for deleting entities with join. + /// + internal static void DeleteEntitiesWithJoin(ISqlSugarClient db) + { + // 联表删除学生实体 + // Delete student entities with join + db.Deleteable() + .Where(p => SqlFunc.Subqueryable().Where(s => s.Id == p.SchoolId).Any()) + .ExecuteCommand(); + } + + [SugarTable("Students_a1")] + public class Student + { + [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] // 主键 + public int Id { get; set; } + + public string Name { get; set; } + + public int SchoolId { get; set; } + } + + [SugarTable("Orders_a2")] + public class Order + { + [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] // 主键 + public int Id { get; set; } + + public string OrderNumber { get; set; } + + public decimal Amount { get; set; } + } + } +} \ No newline at end of file diff --git a/Src/Asp.NetCore2/SqliteTest/a2_Sql.cs b/Src/Asp.NetCore2/SqliteTest/a2_Sql.cs new file mode 100644 index 000000000..ec9efb782 --- /dev/null +++ b/Src/Asp.NetCore2/SqliteTest/a2_Sql.cs @@ -0,0 +1,74 @@ +using SqlSugar; +using System; +using System.Collections.Generic; + +namespace OrmTest +{ + internal class _a2_Sql + { + /// + /// 初始化 SQL 操作的示例方法。 + /// Initializes example methods for SQL operations. + /// + internal static void Init() + { + // 获取新的数据库连接对象 + // Get a new database connection object + var db = DbHelper.GetNewDb(); + + // CodeFirst 初始化 ClassA 表 + // CodeFirst initializes the ClassA table + db.CodeFirst.InitTables(); + db.Insertable(new ClassA() { Name = Guid.NewGuid().ToString("N") }).ExecuteCommand(); + + // 1. 无参数查询 DataTable + // 1. Query DataTable without parameters + var dt1 = db.Ado.GetDataTable("SELECT * FROM Table_a2"); + + // 2. 带参数查询 DataTable(简化用法) + // 2. Query DataTable with parameters (simplified usage) + var dt2 = db.Ado.GetDataTable("SELECT * FROM Table_a2 WHERE id=@id AND name LIKE @name", + new { id = 1, name = "%Jack%" }); + + // 3. 带参数查询 DataTable(复杂用法) + // 3. Query DataTable with parameters (complex usage) + var parameters = new List + { + new SugarParameter("@id", 1), + new SugarParameter("@name", "%Jack%",System.Data.DbType.AnsiString)//DbType + }; + var dt3 = db.Ado.GetDataTable("SELECT * FROM Table_a2 WHERE id=@id AND name LIKE @name", parameters); + + // 4. 带参数查询 DataTable(结合用法) + // 4. Query DataTable with parameters (combined usage) + var dynamicParameters = db.Ado.GetParameters(new { p = 1, p2 = "A" }); + var dt4 = db.Ado.GetDataTable("SELECT * FROM Table_a2 WHERE id=@p AND name=@p2", dynamicParameters); + + // 原生 SQL 使用实体进行查询 + // Native SQL query using an entity + List entities = db.Ado.SqlQuery("SELECT * FROM Table_a2"); + List entities2 = db.Ado.SqlQuery("SELECT * FROM Table_a2 WHERE ID>@ID",new { ID=1}); + + // 原生 SQL 使用匿名对象进行查询 + // Native SQL query using an anonymous object + List anonymousObjects = db.Ado.SqlQuery("SELECT * FROM Table_a2"); + + // 执行 SQL 命令(插入、更新、删除操作) + // Execute SQL commands (insert, update, delete operations) + db.Ado.ExecuteCommand("INSERT INTO Table_a2 (name) VALUES ( 'New Record')"); + } + + /// + /// 示例实体类。 + /// Example entity class. + /// + [SugarTable("Table_a2")] + public class ClassA + { + [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] + public int Id { get; set; } + + public string Name { get; set; } + } + } +} \ No newline at end of file diff --git a/Src/Asp.NetCore2/SqliteTest/a3_Merge.cs b/Src/Asp.NetCore2/SqliteTest/a3_Merge.cs new file mode 100644 index 000000000..561dd248c --- /dev/null +++ b/Src/Asp.NetCore2/SqliteTest/a3_Merge.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + internal class _a3_Merge + { + // 中文备注:初始化方法(用于大数据处理) + // English Comment: Initialization method (for big data processing) + internal static void Init() + { + var db = DbHelper.GetNewDb(); + //建表 + //Create table + db.CodeFirst.InitTables(); + var list = new List() { new Order() { Name = "jack" } }; + + // 中文备注:执行插入或更新操作 + // English Comment: Perform insert or update operation + db.Storageable(list).ExecuteCommand(); + + // 中文备注:分页执行插入或更新操作,每页1000条记录 + // English Comment: Perform insert or update operation with paging, 1000 records per page + db.Storageable(list).PageSize(1000).ExecuteCommand(); + + // 中文备注:带异常处理的分页插入或更新操作,每页1000条记录 + // English Comment: Perform insert or update operation with exception handling and paging, 1000 records per page + db.Storageable(list).PageSize(1000, exrows => { }).ExecuteCommand(); + + // 中文备注:使用Fastest方式批量合并数据(用于大数据处理) + // English Comment: Merge data using Fastest method (for big data processing) + db.Fastest().BulkMerge(list); + + // 中文备注:分页使用Fastest方式批量合并数据,每页100000条记录(用于大数据处理) + // English Comment: Merge data using Fastest method with paging, 100000 records per page (for big data processing) + db.Fastest().PageSize(100000).BulkMerge(list); + } + + [SqlSugar.SugarTable("Order_a3")] + public class Order + { + [SqlSugar.SugarColumn(IsPrimaryKey =true,IsIdentity =true)] + public int Id { get; set; } + public string Name { get; set; } + // 其他属性 + } + } +} \ No newline at end of file