SqlSugar/Src/Asp.NetCore2/OracleTest/8_Insert.cs

57 lines
2.5 KiB
C#
Raw Normal View History

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