From 3e29e8696124460b39cbc883f461aa7a79d054d8 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Tue, 1 Apr 2025 13:29:10 +0800 Subject: [PATCH] Add demo --- .../UserTestCases/UnitTest/Main.cs | 1 + .../UserTestCases/UnitTest/Unitdfafayydd.cs | 263 ++++++++++++++++++ 2 files changed, 264 insertions(+) create mode 100644 Src/Asp.NetCore2/SqlSeverTest/UserTestCases/UnitTest/Unitdfafayydd.cs diff --git a/Src/Asp.NetCore2/SqlSeverTest/UserTestCases/UnitTest/Main.cs b/Src/Asp.NetCore2/SqlSeverTest/UserTestCases/UnitTest/Main.cs index ce3bb9c4a..535f25343 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/UserTestCases/UnitTest/Main.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/UserTestCases/UnitTest/Main.cs @@ -33,6 +33,7 @@ namespace OrmTest } public static void Init() { + Unitdfafassfa.Init(); Unitasdfays.Init(); Unitsadfadsayss.Init(); Unitadfsa1ysfds.Init(); diff --git a/Src/Asp.NetCore2/SqlSeverTest/UserTestCases/UnitTest/Unitdfafayydd.cs b/Src/Asp.NetCore2/SqlSeverTest/UserTestCases/UnitTest/Unitdfafayydd.cs new file mode 100644 index 000000000..4ad85ab88 --- /dev/null +++ b/Src/Asp.NetCore2/SqlSeverTest/UserTestCases/UnitTest/Unitdfafayydd.cs @@ -0,0 +1,263 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Linq; +using System.Reflection; +using SqlSugar; +using static System.Net.Mime.MediaTypeNames; +namespace OrmTest +{ + + public class Unitdfafassfa + { + + public static void Init() + + + + { + + var db = NewUnitTest.Db; + + + + + + db.Aop.OnLogExecuting = (x, y) => + + { + + Console.WriteLine(UtilMethods.GetNativeSql(x, y)); + + }; + + //建表 + + db.CodeFirst.InitTables(); + + db.CodeFirst.InitTables(); + + db.CodeFirst.InitTables(); + + //清空表 + + db.DbMaintenance.TruncateTable(); + + db.DbMaintenance.TruncateTable(); + + db.DbMaintenance.TruncateTable(); + + var insertdata1 = new List() { new dc_test_book() { BookId = 1, Monery = 100, UserType = "VIP贵宾", BookName = "三国演义" }, new dc_test_book() { BookId = 1, Monery = 200, UserType = "普通用户", BookName = "三国演义" } }; + + var insertdata2 = new List() { new dc_test_order() { BookId = 1, OrderNumber = "A早1122", Rbo = "小明" } }; + + var insertdata3 = new List() { new dc_test_rbo() { Rbo = "小明", Type = "VIP贵宾" } }; + + //插入测试数据 + + + + var result = db.Insertable(insertdata1).ExecuteCommand();//用例代码 + + result = db.Insertable(insertdata2).ExecuteCommand();//用例代码 + + result = db.Insertable(insertdata3).ExecuteCommand();//用例代码 + + var data = db.Queryable().InnerJoin((a, b) => a.Rbo == b.Rbo).Select((a, b) => new + + { + + OrderNumber = a.OrderNumber, + + OrderDetails = SqlFunc.Subqueryable().Where(c => c.BookId == a.BookId && c.UserType == b.Type) + + .ToList(c => new OrderDetails() + { + + BookName = c.BookName, + + Monery = c.Monery, + + OrderNumber = a.OrderNumber, + + RboType = b.Type, + + Rbo = b.Rbo + + }) + + }).ToList(); + + Console.WriteLine($"订单详情有:{data.First().OrderDetails.Count()}条"); + + + + Console.WriteLine("用例跑完"); + + + + } + + + + //建类 + + + + public class dc_test_book + + { + + [SugarColumn(ColumnName = "book_id")] + + public int BookId { get; set; } + + + + [SugarColumn(ColumnDataType = "float", ColumnName = "monery")] + + public float Monery { get; set; } + + [SugarColumn(ColumnDataType = "nvarchar(50)", ColumnName = "user_type")] + + public string UserType { get; set; } + + [SugarColumn(ColumnDataType = "nvarchar(50)", ColumnName = "book_name")] + + public string BookName { get; set; } + + } + + public class dc_test_order + + { + + [SugarColumn(ColumnDataType = "nvarchar(50)", ColumnName = "order_number")] + + public string OrderNumber { get; set; } + + [SugarColumn(ColumnDataType = "nvarchar(50)", ColumnName = "rbo")] + + public string Rbo { get; set; } + + [SugarColumn(ColumnName = "book_id")] + + public int BookId { get; set; } + + } + + public class dc_test_rbo + + { + + [SugarColumn(ColumnDataType = "nvarchar(50)", ColumnName = "rbo")] + + public string Rbo { get; set; } + + + + [SugarColumn(ColumnDataType = "nvarchar(50)", ColumnName = "type")] + + public string Type { get; set; } + + } + + public class OrderDetails + { + + public string OrderNumber { get; set; } + + public string Rbo { get; set; } + + public string RboType { get; set; } + + public string BookName { get; set; } + + public float Monery { get; set; } + + } + + + + /// + + /// Helper class for database operations + + /// 数据库操作的辅助类 + + /// + + public class DbHelper + + { + + /// + + /// Database connection string + + /// 数据库连接字符串 + + /// + + public readonly static string Connection = ""; + + + + /// + + /// Get a new SqlSugarClient instance with specific configurations + + /// 获取具有特定配置的新 SqlSugarClient 实例 + + /// + + /// SqlSugarClient instance + + public static SqlSugarClient GetNewDb() + + { + + var db = new SqlSugarClient(new ConnectionConfig() + + { + + IsAutoCloseConnection = true, + + DbType = SqlSugar.DbType.SqlServer, + + 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