SqlSugar/Src/Asp.Net/SqlServerTest/Demo/Demo3_Insertable.cs

63 lines
2.3 KiB
C#
Raw Normal View History

2019-05-20 17:38:22 +08:00
using SqlSugar;
using System;
2019-05-20 17:32:13 +08:00
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OrmTest
{
public class Demo3_Insertable
{
2019-05-20 17:42:46 +08:00
public static void Init()
{
2019-05-20 17:38:22 +08:00
Console.WriteLine("");
Console.WriteLine("#### Insertable Start ####");
2019-05-20 17:32:13 +08:00
2019-05-20 17:38:22 +08:00
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
{
DbType = DbType.SqlServer,
ConnectionString = Config.ConnectionString,
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true,
AopEvents = new AopEvents
{
OnLogExecuting = (sql, p) =>
{
Console.WriteLine(sql);
Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)));
}
}
});
2019-06-01 13:44:31 +08:00
var insertObj = new Order() { Id = 1, Name = "order1",Price=0 };
2020-10-22 18:23:17 +08:00
var insertObjs = new List<Order> {
2019-06-01 13:44:31 +08:00
new Order() { Id = 11, Name = "order11", Price=0 },
new Order() { Id = 12, Name = "order12" , Price=0}
2019-05-20 17:38:22 +08:00
};
2019-06-01 13:44:31 +08:00
//Ignore CreateTime
db.Insertable(insertObj).IgnoreColumns(it => new { it.CreateTime }).ExecuteReturnIdentity();//get identity
db.Insertable(insertObj).IgnoreColumns("CreateTime").ExecuteReturnIdentity();
2019-05-20 17:38:22 +08:00
//Only insert Name and Price
db.Insertable(insertObj).InsertColumns(it => new { it.Name, it.Price }).ExecuteReturnIdentity();
2019-06-01 13:44:31 +08:00
db.Insertable(insertObj).InsertColumns("Name", "Price").ExecuteReturnIdentity();
2019-05-20 17:38:22 +08:00
//ignore null columns
2020-10-22 18:23:17 +08:00
db.Insertable(insertObjs).ExecuteCommand();//get change row count
2019-05-20 17:38:22 +08:00
//Use Lock
db.Insertable(insertObj).With(SqlWith.UpdLock).ExecuteCommand();
2020-10-22 18:23:17 +08:00
insertObjs = new List<Order> {
new Order() { Id = 11, Name = "order11", Price=1 },
new Order() { Id = 12, Name = "order12" , Price=20, CreateTime=DateTime.Now, CustomId=1}
};
db.Insertable(insertObjs).UseSqlServer().ExecuteBlueCopy();
2019-05-20 17:38:22 +08:00
Console.WriteLine("#### Insertable End ####");
2019-05-20 17:32:13 +08:00
}
}
}