注意:
Oracle反回自增列 在字段特性SugarColumn 可以设置序列名称
SqlSever插量插入慢一般是建表语句带有Wtih(索引设置)引起的,把With删掉会很快
插入并返回受影响行数用ExecuteCommand
var t2 = db.Insertable(insertObj).ExecuteCommand();
插入并返回自增列用ExecuteReutrnIdentity
int t30 = db.Insertable(insertObj).ExecuteReturnIdentity(); long t31 = db.Insertable(insertObj).ExecuteReturnBigIdentity(); //4.5.0.2 + long
4.2.3插入并返回实体 , 只是自identity 添加到 参数的实体里面并返回,没有查2次库,所以有些默认值什么的变动是取不到的你们需要手动进行2次查询获取
var t3 = db.Insertable(insertObj).ExecuteReturnEntity();
4.5.0.2 插入并返回bool, 并将identity赋值到实体
var t3 = db.Insertable(insertObj).ExecuteCommandIdentityIntoEntity();
只插入列 Name和SchoolId
var t4 = db.Insertable(insertObj).InsertColumns(it => new { it.Name, it.SchoolId }).ExecuteReturnIdentity();不插入列 Name和TestId
var t5 = db.Insertable(insertObj).IgnoreColumns(it => new { it.Name, it.TestId }).ExecuteReturnIdentity();根据条件指定不插入列
var t6 = db.Insertable(insertObj).IgnoreColumns(it => it == "Name" || it == "TestId").ExecuteReturnIdentity();
List中所有列不插入
var t61 = db.Insertable(updateObj) .IgnoreColumns(it => list.Contains(it) ).ExecuteCommand();
使用锁
var t8 = db.Insertable(insertObj).With(SqlWith.UpdLock).ExecuteCommand();
可以设置NULL列不插入和是否强制插入自增列
var t9 = db.Insertable(insertObj2) .Where(true/* Is no insert null */, true/*off identity*/) .ExecuteCommand();
批量插入(性能很快不用操心)
var insertObjs = new List<Student>(); var s9 = db.Insertable(insertObjs.ToArray()).ExecuteCommand(); //注意 : SqlSever 建表语句带有Wtih(设置),如果设置不合理,可能会引起慢,把With删掉就会很快
4.2.2 匿名对象和字典的支持
var t12 = db.Insertable<Student>(new { Name = "a" }).ExecuteCommand();
//INSERT INTO [STudent] ([Name]) VALUES ('a') ;SELECT SCOPE_IDENTITY();
var t13 = db.Insertable<Student>(new Dictionary<string, object>() {{ "name","a"} }).ExecuteCommand();
//INSERT INTO [STudent] ([Name]) VALUES ('a') ;SELECT SCOPE_IDENTITY();4.6.4.8支持了不加泛型的字典
var dt = new Dictionary<string, object>();
dt.Add("name", "1");
var t66 = db.Insertable(dt).AS("student").ExecuteReturnIdentity();4.6.1 插入数据库默认值
我们只要在实体上加上
[SugarColumn(IsOnlyIgnoreInsert=true)]
public DateTime CreateTime { get; set; }将A表数据插入B表
db.Insertable(db.Queryable<A>().Select<B>().ToList()).ExecuteCommand();
2016 © donet5.comApache Licence 2.0