You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
2.3 KiB
65 lines
2.3 KiB
using SqlSugar;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace OrmTest
|
|
{
|
|
public class Demo3_Insertable
|
|
{
|
|
public static void Init()
|
|
{
|
|
Console.WriteLine("");
|
|
Console.WriteLine("#### Insertable Start ####");
|
|
|
|
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
|
|
{
|
|
DbType = DbType.MySql,
|
|
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)));
|
|
}
|
|
}
|
|
});
|
|
|
|
var insertObj = new Order() { Id = 1, Name = "order1",Price=0 };
|
|
var updateObjs = new List<Order> {
|
|
new Order() { Id = 11, Name = "order11", Price=0 },
|
|
new Order() { Id = 12, Name = "order12" , Price=0}
|
|
};
|
|
|
|
//Ignore CreateTime
|
|
db.Insertable(insertObj).IgnoreColumns(it => new { it.CreateTime }).ExecuteReturnIdentity();//get identity
|
|
db.Insertable(insertObj).IgnoreColumns("CreateTime").ExecuteReturnIdentity();
|
|
|
|
//Only insert Name and Price
|
|
db.Insertable(insertObj).InsertColumns(it => new { it.Name, it.Price }).ExecuteReturnIdentity();
|
|
db.Insertable(insertObj).InsertColumns("Name", "Price").ExecuteReturnIdentity();
|
|
|
|
//ignore null columns
|
|
db.Insertable(updateObjs).ExecuteCommand();//get change row count
|
|
|
|
//Use Lock
|
|
db.Insertable(insertObj).With(SqlWith.UpdLock).ExecuteCommand();
|
|
|
|
db.Deleteable<Order>().ExecuteCommand();
|
|
db.Insertable(new Order()
|
|
{
|
|
CreateTime = DateTime.Now,
|
|
CustomId = 11,
|
|
Name = "11",
|
|
Price = 11
|
|
}).UseMySql().ExecuteBlueCopy();
|
|
var data = db.Queryable<Order>().ToList();
|
|
Console.WriteLine("#### Insertable End ####");
|
|
}
|
|
}
|
|
}
|
|
|