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)));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2020-10-25 18:59:46 +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> {
|
2021-11-18 02:39:29 +08:00
|
|
|
|
new Order() { Id = 11, Name = "XX", Price=0 },
|
|
|
|
|
new Order() { Id = 12, Name = "XX2" , Price=0}
|
2019-05-20 17:38:22 +08:00
|
|
|
|
};
|
|
|
|
|
|
2021-08-05 12:27:33 +08:00
|
|
|
|
var x=db.Insertable(insertObjs).RemoveDataCache().IgnoreColumns(it=>it.CreateTime).UseParameter().ExecuteCommand();
|
2021-08-05 00:07:24 +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}
|
|
|
|
|
};
|
2021-08-30 19:07:38 +08:00
|
|
|
|
db.Insertable(insertObjs).UseSqlServer().ExecuteBulkCopy();
|
2020-12-18 21:53:27 +08:00
|
|
|
|
var dt = db.Queryable<Order>().Take(5).ToDataTable();
|
|
|
|
|
dt.TableName = "Order";
|
2021-08-30 19:07:38 +08:00
|
|
|
|
db.Insertable(dt).UseSqlServer().ExecuteBulkCopy();
|
2020-11-13 21:36:52 +08:00
|
|
|
|
db.CodeFirst.InitTables<RootTable0, TwoItem, TwoItem2, TwoItem3>();
|
|
|
|
|
db.CodeFirst.InitTables<ThreeItem2>();
|
|
|
|
|
db.DbMaintenance.TruncateTable("RootTable0");
|
|
|
|
|
db.DbMaintenance.TruncateTable("TwoItem");
|
|
|
|
|
db.DbMaintenance.TruncateTable("TwoItem2");
|
|
|
|
|
db.DbMaintenance.TruncateTable("TwoItem3");
|
|
|
|
|
db.DbMaintenance.TruncateTable("ThreeItem2");
|
2020-10-25 20:17:52 +08:00
|
|
|
|
Console.WriteLine("SubInsert Start");
|
|
|
|
|
|
2022-12-17 10:46:45 +08:00
|
|
|
|
|
2021-10-30 13:26:03 +08:00
|
|
|
|
var dict = new Dictionary<string, object>();
|
|
|
|
|
dict.Add("name", "1");
|
|
|
|
|
dict.Add("CreateTime", DateTime.Now);
|
|
|
|
|
dict.Add("Price", 1);
|
|
|
|
|
db.Insertable(dict).AS("[Order]").ExecuteCommand();
|
2021-11-18 02:39:29 +08:00
|
|
|
|
|
2022-03-22 19:18:11 +08:00
|
|
|
|
db.Insertable(new List<Order>()).UseParameter().ExecuteCommand();
|
2021-11-18 02:39:29 +08:00
|
|
|
|
|
2021-11-21 14:29:32 +08:00
|
|
|
|
db.Fastest<Order>().BulkCopy(insertObjs);
|
2022-01-05 11:22:05 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var dataTable= db.Queryable<Order>().Select("id,name,Price").Take(2).ToDataTable();
|
2022-04-30 18:32:37 +08:00
|
|
|
|
int result= db.Fastest<System.Data.DataTable>().AS("order").BulkCopy("order", dataTable);
|
|
|
|
|
int result2 = db.Fastest<System.Data.DataTable>().AS("order").BulkCopy( dataTable);
|
2022-12-26 14:04:55 +08:00
|
|
|
|
|
|
|
|
|
object o = db.Queryable<Order>().First();
|
2023-02-15 15:30:42 +08:00
|
|
|
|
db.InsertableByObject(o).ExecuteCommandAsync().GetAwaiter().GetResult();
|
2023-04-02 11:29:03 +08:00
|
|
|
|
db.InsertableByObject(o).ExecuteReturnIdentityAsync().GetAwaiter().GetResult();
|
2022-12-26 14:04:55 +08:00
|
|
|
|
object os = db.Queryable<Order>().Take(2).ToList();
|
|
|
|
|
db.InsertableByObject(os).ExecuteCommand();
|
2023-01-12 19:35:28 +08:00
|
|
|
|
|
2023-06-03 19:36:46 +08:00
|
|
|
|
|
|
|
|
|
db.InsertableByObject(o).IgnoreColumns("CustomId").ExecuteCommand();
|
|
|
|
|
db.InsertableByObject(o).IgnoreColumns("CustomId").ExecuteCommandAsync().GetAwaiter().GetResult();
|
|
|
|
|
var id=db.InsertableByObject(o).IgnoreColumns("CustomId").ExecuteReturnIdentity();
|
|
|
|
|
var id2 = db.InsertableByObject(o).IgnoreColumns("CustomId").ExecuteReturnIdentityAsync().GetAwaiter().GetResult();
|
2023-01-12 19:35:28 +08:00
|
|
|
|
db.CodeFirst.InitTables<City>();
|
2020-11-13 23:31:09 +08:00
|
|
|
|
Console.WriteLine("#### Insertable End ####");
|
|
|
|
|
|
|
|
|
|
}
|
2020-11-13 21:36:52 +08:00
|
|
|
|
|
2022-12-26 14:04:55 +08:00
|
|
|
|
|
2019-05-20 17:32:13 +08:00
|
|
|
|
}
|
|
|
|
|
}
|