Update oracle demo

This commit is contained in:
sunkaixuan 2023-11-07 20:49:14 +08:00
parent 3ad11e1432
commit 605b5e2b1b
11 changed files with 79 additions and 82 deletions

View File

@ -42,7 +42,7 @@ namespace OrmTest
UserName="admin", UserName="admin",
RegistrationDate=DateTime.Now, RegistrationDate=DateTime.Now,
}).ExecuteReturnIdentity(); }).ExecuteReturnSnowflakeId();
//Query //Query
//查询 //查询
@ -67,8 +67,8 @@ namespace OrmTest
/// User ID (Primary Key) /// User ID (Primary Key)
/// 用户ID主键 /// 用户ID主键
/// </summary> /// </summary>
[SugarColumn(IsIdentity = true, IsPrimaryKey = true)] [SugarColumn(IsPrimaryKey = true)]
public int UserId { get; set; } public long UserId { get; set; }
/// <summary> /// <summary>
/// User name /// User name
@ -118,8 +118,8 @@ namespace OrmTest
/// User ID (Primary Key) /// User ID (Primary Key)
/// 用户ID主键 /// 用户ID主键
/// </summary> /// </summary>
[SugarColumn(IsIdentity = true,ColumnName ="Id", IsPrimaryKey = true)] [SugarColumn(ColumnName ="Id", IsPrimaryKey = true)]
public int UserId { get; set; } public long UserId { get; set; }
/// <summary> /// <summary>
/// User name /// User name

View File

@ -61,7 +61,7 @@ namespace OrmTest
SqlSugarClient db = DbHelper.GetNewDb(); SqlSugarClient db = DbHelper.GetNewDb();
db.CodeFirst.InitTables<Student03>(); db.CodeFirst.InitTables<Student03>();
db.Insertable(new Student03() { Name = "name" + SnowFlakeSingle.Instance.NextId() }) db.Insertable(new Student03() { Name = "name" + SnowFlakeSingle.Instance.NextId() })
.ExecuteCommand(); .ExecuteReturnSnowflakeId();
} }
/// <summary> /// <summary>
@ -183,8 +183,8 @@ namespace OrmTest
public class Student03 public class Student03
{ {
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)] [SugarColumn(IsPrimaryKey = true)]
public int Id { get; set; } public long Id { get; set; }
public string Name { get; set; } public string Name { get; set; }
} }
} }

View File

@ -121,8 +121,8 @@ namespace OrmTest
[SqlSugar.SugarTable("Order04")] [SqlSugar.SugarTable("Order04")]
public class Order public class Order
{ {
[SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true)] [SqlSugar.SugarColumn( IsIdentity = true)]
public int Id { get; set; } public long Id { get; set; }
public string Name { get; set; } public string Name { get; set; }
public int CustomId { get; set; } public int CustomId { get; set; }
// 其他订单相关属性... // 其他订单相关属性...
@ -135,9 +135,9 @@ namespace OrmTest
[SqlSugar.SugarTable("OrderDetail04")] [SqlSugar.SugarTable("OrderDetail04")]
public class OrderDetail public class OrderDetail
{ {
[SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true)] [SqlSugar.SugarColumn(IsPrimaryKey = true)]
public int Id { get; set; } public long Id { get; set; }
public int OrderId { get; set; } public long OrderId { get; set; }
// 其他订单详情相关属性... // 其他订单详情相关属性...
} }
@ -148,8 +148,8 @@ namespace OrmTest
[SqlSugar.SugarTable("Custom04")] [SqlSugar.SugarTable("Custom04")]
public class Custom public class Custom
{ {
[SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true)] [SqlSugar.SugarColumn(IsPrimaryKey = true )]
public int Id { get; set; } public long Id { get; set; }
public string Name { get; set; } public string Name { get; set; }
// 其他客户相关属性... // 其他客户相关属性...
} }
@ -162,7 +162,7 @@ namespace OrmTest
/// </summary> /// </summary>
public class ViewOrder2 public class ViewOrder2
{ {
public int Id { get; set; } public long Id { get; set; }
public string Name { get; set; } public string Name { get; set; }
public string CustomName { get; set; } public string CustomName { get; set; }
// 其他类1相关属性... // 其他类1相关属性...

View File

@ -37,18 +37,18 @@ public class _5_PageQuery
// 添加学校数据 // 添加学校数据
// Add school data // Add school data
var school1 = new School { Name = "School A" }; var school1 = new School { Id=1, Name = "School A" };
var school2 = new School { Name = "School B" }; var school2 = new School {Id=2, Name = "School B" };
db.Insertable(school1).ExecuteCommand(); var id1=db.Insertable(school1).ExecuteReturnSnowflakeId();
db.Insertable(school2).ExecuteCommand(); var id2=db.Insertable(school2).ExecuteReturnSnowflakeId();
// 添加学生数据 // 添加学生数据
// Add student data // Add student data
var student1 = new Student { SchoolId = school1.Id, Name = "John", CreateTime = DateTime.Now }; var student1 = new Student { SchoolId =id1, Name = "John", CreateTime = DateTime.Now };
var student2 = new Student { SchoolId = school1.Id, Name = "Alice", CreateTime = DateTime.Now }; var student2 = new Student { SchoolId =id2, Name = "Alice", CreateTime = DateTime.Now };
db.Insertable(student1).ExecuteCommand(); db.Insertable(student1).ExecuteReturnSnowflakeId();
db.Insertable(student2).ExecuteCommand(); db.Insertable(student2).ExecuteReturnSnowflakeId();
Console.WriteLine("Test data added successfully."); Console.WriteLine("Test data added successfully.");
} }
@ -96,17 +96,17 @@ public class _5_PageQuery
[SugarTable("Student05")] [SugarTable("Student05")]
public class Student public class Student
{ {
[SugarColumn(IsIdentity =true,IsPrimaryKey =true)] [SugarColumn(IsPrimaryKey =true)]
public int Id { get; set; } public long Id { get; set; }
public int SchoolId { get; set; } public long SchoolId { get; set; }
public string Name { get; set; } public string Name { get; set; }
public DateTime CreateTime { get; set; } public DateTime CreateTime { get; set; }
} }
[SugarTable("School05")] [SugarTable("School05")]
public class School public class School
{ {
[SugarColumn(IsIdentity = true, IsPrimaryKey = true)] [SugarColumn( IsPrimaryKey = true)]
public int Id { get; set; } public long Id { get; set; }
public string Name { get; set; } public string Name { get; set; }
} }
} }

View File

@ -95,7 +95,7 @@ namespace OrmTest
// One-to-one navigation query with condition, query table Student and include its associated School with specific SchoolId. // One-to-one navigation query with condition, query table Student and include its associated School with specific SchoolId.
// 带条件的一对一导航查询查询Student表并包含其关联的School表条件为特定的SchoolId。 // 带条件的一对一导航查询查询Student表并包含其关联的School表条件为特定的SchoolId。
var list = db.Queryable<Student>() var list = db.Queryable<Student>()
.Where(it => it.School.SchoolId == 1) .Where(it => it.School.SchoolId > 1)
.ToList(); .ToList();
// Inner join navigation query, query table Student and include its associated School. // Inner join navigation query, query table Student and include its associated School.
@ -176,11 +176,11 @@ namespace OrmTest
[SugarTable("Student06")] [SugarTable("Student06")]
public class Student public class Student
{ {
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)] [SugarColumn(IsPrimaryKey = true)]
public int StudentId { get; set; } public long StudentId { get; set; }
public string Name { get; set; } public string Name { get; set; }
public string SexCode { get; set; } public string SexCode { get; set; }
public int SchoolId { get; set; } public long SchoolId { get; set; }
// One-to-One navigation property to School entity. // One-to-One navigation property to School entity.
// 与School实体的一对一导航属性。 // 与School实体的一对一导航属性。
@ -200,8 +200,8 @@ namespace OrmTest
[SugarTable("School06")] [SugarTable("School06")]
public class School public class School
{ {
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)] [SugarColumn(IsPrimaryKey = true)]
public int SchoolId { get; set; } public long SchoolId { get; set; }
public string SchoolName { get; set; } public string SchoolName { get; set; }
} }
@ -212,10 +212,10 @@ namespace OrmTest
[SugarTable("Book06")] [SugarTable("Book06")]
public class Book public class Book
{ {
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)] [SugarColumn(IsPrimaryKey = true)]
public int BookId { get; set; } public long BookId { get; set; }
public string Name { get; set; } public string Name { get; set; }
public int StudentId { get; set; } public long StudentId { get; set; }
} }
/// <summary> /// <summary>
@ -225,8 +225,8 @@ namespace OrmTest
[SugarTable("A06")] [SugarTable("A06")]
public class A public class A
{ {
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)] [SugarColumn(IsPrimaryKey = true)]
public int AId { get; set; } public long AId { get; set; }
public string Name { get; set; } public string Name { get; set; }
// Many-to-Many navigation property to B entities using ABMapping table. // Many-to-Many navigation property to B entities using ABMapping table.
@ -242,8 +242,8 @@ namespace OrmTest
[SugarTable("B06")] [SugarTable("B06")]
public class B public class B
{ {
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)] [SugarColumn(IsPrimaryKey = true) ]
public int BId { get; set; } public long BId { get; set; }
public string Name { get; set; } public string Name { get; set; }
// Many-to-Many navigation property to A entities using ABMapping table. // Many-to-Many navigation property to A entities using ABMapping table.
@ -260,9 +260,9 @@ namespace OrmTest
public class ABMapping public class ABMapping
{ {
[SugarColumn(IsPrimaryKey = true)] [SugarColumn(IsPrimaryKey = true)]
public int AId { get; set; } public long AId { get; set; }
[SugarColumn(IsPrimaryKey = true)] [SugarColumn(IsPrimaryKey = true)]
public int BId { get; set; } public long BId { get; set; }
} }
} }
} }

View File

@ -65,8 +65,8 @@ namespace OrmTest
[SqlSugar.SugarTable("Student07")] [SqlSugar.SugarTable("Student07")]
public class Student public class Student
{ {
[SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true)] [SqlSugar.SugarColumn(IsPrimaryKey = true)]
public int Id { get; set; } // 学生ID (Student ID) public long Id { get; set; } // 学生ID (Student ID)
public string Name { get; set; } // 学生姓名 (Student Name) public string Name { get; set; } // 学生姓名 (Student Name)
public int Age { get; set; } // 学生年龄 (Student Age) public int Age { get; set; } // 学生年龄 (Student Age)
} }

View File

@ -15,27 +15,26 @@ namespace OrmTest
var db = DbHelper.GetNewDb(); var db = DbHelper.GetNewDb();
// 初始化实体表格Initialize entity tables // 初始化实体表格Initialize entity tables
db.CodeFirst.InitTables<StudentWithIdentity, StudentWithSnowflake>(); db.CodeFirst.InitTables<StudentWithSnowflake>();
// Use Case 1: 返回插入行数Return the number of inserted rows // Use Case 1: 返回插入行数Return the number of inserted rows
var rowCount = db.Insertable(new StudentWithIdentity() { Name = "name" }).ExecuteCommand(); var rowCount = db.Insertable(new StudentWithSnowflake() {Id=SnowFlakeSingle.Instance.NextId(), Name = "name" }).ExecuteCommand();
// Use Case 2: 插入数据并返回自增列Insert data and return the auto-incremented column ////Oracle 12C+
var identity = db.Insertable(new StudentWithIdentity() { Name = "name2" }).ExecuteReturnIdentity(); //// Use Case 2: 插入数据并返回自增列Insert data and return the auto-incremented column
//var identity = db.Insertable(new StudentWithSnowflake() { Name = "name2" }).ExecuteReturnIdentity();
// Use Case 3: 返回雪花IDReturn the snowflake ID // Use Case 3: 返回雪花IDReturn the snowflake ID
var snowflakeId = db.Insertable(new StudentWithSnowflake() { Name = "name" }).ExecuteReturnSnowflakeId(); var snowflakeId = db.Insertable(new StudentWithSnowflake() { Name = "name" }).ExecuteReturnSnowflakeId();
// Use Case 4: 强制设置表名别名Forcefully set table name alias // Use Case 4: 强制设置表名别名Forcefully set table name alias
db.Insertable(new StudentWithIdentity() { Name = "name2" }).AS("StudentWithIdentity").ExecuteCommand(); db.Insertable(new StudentWithSnowflake() { Name = "name2" }).AS("StudentWithSnowflake08").ExecuteReturnSnowflakeId();
// Use Case 5: 批量插入实体非参数化插入Batch insert entities (non-parameterized) // Use Case 5: 批量插入实体非参数化插入Batch insert entities (non-parameterized)
var list = db.Queryable<StudentWithIdentity>().Take(2).ToList(); var list = db.Queryable<StudentWithSnowflake>().Take(2).ToList();
db.Insertable(list).ExecuteCommand(); db.Insertable(list).ExecuteReturnSnowflakeIdList();
db.Insertable(list).PageSize(1000).ExecuteCommand(); db.Insertable(list).PageSize(1000).ExecuteReturnSnowflakeIdList();
// 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 // Use Case 7: 大数据写入示例代码请根据实际情况调整Bulk data insertion - Example code, adjust based on actual scenario
var listLong = new List<StudentWithSnowflake>() { var listLong = new List<StudentWithSnowflake>() {
@ -45,14 +44,6 @@ namespace OrmTest
db.Fastest<StudentWithSnowflake>().BulkCopy(listLong); db.Fastest<StudentWithSnowflake>().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 // 实体类带雪花主键Entity class: With snowflake primary key
[SugarTable("StudentWithSnowflake08")] [SugarTable("StudentWithSnowflake08")]

View File

@ -11,7 +11,7 @@ namespace OrmTest
//Each example will automatically create a table and can run independently. //Each example will automatically create a table and can run independently.
//每个例子都会自动建表 并且可以独立运行 //每个例子都会自动建表 并且可以独立运行
_1_CodeFirst.Init(); _1_CodeFirst.Init();
_2_DbFirst.Init(); //_2_DbFirst.Init();
_3_EasyQuery.Init(); _3_EasyQuery.Init();
_4_JoinQuery.Init(); _4_JoinQuery.Init();
_5_PageQuery.Init(); _5_PageQuery.Init();
@ -35,7 +35,7 @@ namespace OrmTest
/// Database connection string /// Database connection string
/// 数据库连接字符串 /// 数据库连接字符串
/// </summary> /// </summary>
public readonly static string Connection = "Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=150.158.37.115)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=ORCL)));User Id=HBaa1dfa;Password=Qdies123test;Pooling='true';Max Pool Size=150"; public readonly static string Connection = "Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=150.158.37.115)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=ORCL)));User Id=HB ;Password=Qdies123test;Pooling='true';Max Pool Size=150";
/// <summary> /// <summary>
/// Get a new SqlSugarClient instance with specific configurations /// Get a new SqlSugarClient instance with specific configurations
@ -49,7 +49,12 @@ namespace OrmTest
IsAutoCloseConnection = true, IsAutoCloseConnection = true,
DbType = DbType.Oracle, DbType = DbType.Oracle,
ConnectionString = Connection, ConnectionString = Connection,
LanguageType=LanguageType.Default//Set language LanguageType=LanguageType.Default,//Set language
MoreSettings=new ConnMoreSettings()
{
//Oracle 12+ support identity
//EnableOracleIdentity=true
}
}, },
it => { it => {

View File

@ -164,19 +164,19 @@ namespace OrmTest
[SugarTable("Students_a1")] [SugarTable("Students_a1")]
public class Student public class Student
{ {
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)] // 主键 [SugarColumn(IsPrimaryKey = true)] // 主键
public int Id { get; set; } public long Id { get; set; }
public string Name { get; set; } public string Name { get; set; }
public int SchoolId { get; set; } public long SchoolId { get; set; }
} }
[SugarTable("Orders_a2")] [SugarTable("Orders_a2")]
public class Order public class Order
{ {
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)] // 主键 [SugarColumn(IsPrimaryKey = true)] // 主键
public int Id { get; set; } public long Id { get; set; }
public string OrderNumber { get; set; } public string OrderNumber { get; set; }

View File

@ -19,7 +19,7 @@ namespace OrmTest
// CodeFirst 初始化 ClassA 表 // CodeFirst 初始化 ClassA 表
// CodeFirst initializes the ClassA table // CodeFirst initializes the ClassA table
db.CodeFirst.InitTables<ClassA>(); db.CodeFirst.InitTables<ClassA>();
db.Insertable(new ClassA() { Name = Guid.NewGuid().ToString("N") }).ExecuteCommand(); db.Insertable(new ClassA() { Name = Guid.NewGuid().ToString("N") }).ExecuteReturnSnowflakeId();
// 1. 无参数查询 DataTable // 1. 无参数查询 DataTable
// 1. Query DataTable without parameters // 1. Query DataTable without parameters
@ -55,7 +55,7 @@ namespace OrmTest
// 执行 SQL 命令(插入、更新、删除操作) // 执行 SQL 命令(插入、更新、删除操作)
// Execute SQL commands (insert, update, delete operations) // Execute SQL commands (insert, update, delete operations)
db.Ado.ExecuteCommand("INSERT INTO Table_a2 (name) VALUES ( 'New Record')"); db.Ado.ExecuteCommand("INSERT INTO Table_a2 (id,name) VALUES ("+SnowFlakeSingle.Instance.NextId()+", 'New Record')");
} }
/// <summary> /// <summary>
@ -65,8 +65,8 @@ namespace OrmTest
[SugarTable("Table_a2")] [SugarTable("Table_a2")]
public class ClassA public class ClassA
{ {
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)] [SugarColumn(IsPrimaryKey = true)]
public int Id { get; set; } public long Id { get; set; }
public string Name { get; set; } public string Name { get; set; }
} }

View File

@ -1,4 +1,5 @@
using System; using SqlSugar;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
@ -16,7 +17,7 @@ namespace OrmTest
//建表 //建表
//Create table //Create table
db.CodeFirst.InitTables<Order>(); db.CodeFirst.InitTables<Order>();
var list = new List<Order>() { new Order() { Name = "jack" } }; var list = new List<Order>() { new Order() { Id=SnowFlakeSingle.Instance.NextId(), Name = "jack" } };
// 中文备注:执行插入或更新操作 // 中文备注:执行插入或更新操作
// English Comment: Perform insert or update operation // English Comment: Perform insert or update operation
@ -42,8 +43,8 @@ namespace OrmTest
[SqlSugar.SugarTable("Order_a3")] [SqlSugar.SugarTable("Order_a3")]
public class Order public class Order
{ {
[SqlSugar.SugarColumn(IsPrimaryKey =true,IsIdentity =true)] [SqlSugar.SugarColumn(IsPrimaryKey =true)]
public int Id { get; set; } public long Id { get; set; }
public string Name { get; set; } public string Name { get; set; }
// 其他属性 // 其他属性
} }