public static void Init() { Console.WriteLine(""); Console.WriteLine("#### Saveable Start ####"); 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))); } } }); //insert or update var x = db.Storageable <Order>(new Order() { Id = 1, Name = "jack" }).ToStorage(); x.AsUpdateable.ExecuteCommand(); x.AsInsertable.ExecuteCommand(); Console.WriteLine(""); Console.WriteLine("#### Saveable End ####"); }
/// <summary> /// 插入或更新 /// </summary> /// <param name="conn"></param> /// <param name="objs"></param> /// <param name="identity"></param> /// <returns></returns> public async Task <List <T> > InsertOrUpdateAsync(SqlSugarClient conn, List <T> objs, Expression <Func <T, object> > identityColumn = null) { var storageable = conn.Storageable <T>(objs).Saveable(); if (identityColumn != null) { storageable.WhereColumns(identityColumn); } var storage = storageable.ToStorage(); if (storage.InsertList.Count > 0) { await storage.AsInsertable.ExecuteCommandAsync(); } if (storage.UpdateList.Count > 0) { await storage.AsUpdateable.ExecuteCommandAsync(); } string identity = null; object[] identities = null; if (identityColumn == null) { var entityInfo = _sqlSugarService.CreateConnection(_connName).EntityMaintenance.GetEntityInfo <T>(); var columnInfo = entityInfo.Columns.Where(item => item.IsIdentity).FirstOrDefault(); if (columnInfo != null) { identity = columnInfo.PropertyName; identities = objs.Select(item => columnInfo.PropertyInfo.GetValue(item)).ToArray(); } } else { identity = ((MemberExpression)identityColumn.Body).Member.Name; identities = objs.Select(identityColumn.Compile()).ToArray(); } if (identity == null || identities == null) { return(null); } return(conn.Queryable <T>().In(identity, identities).ToList()); }
public static void Init() { Console.WriteLine(""); Console.WriteLine("#### Saveable Start ####"); 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))); } } }); //insert or update var x = db.Storageable <Order>(new Order() { Id = 1, Name = "jack" }).ToStorage(); x.AsUpdateable.ExecuteCommand(); x.AsInsertable.ExecuteCommand(); var x2 = db.Storageable <Order>(new Order() { Id = 0, Name = "jack" }).ToStorage(); x2.BulkCopy(); x2.BulkUpdate(); var dt = db.Queryable <Order>().Take(1).ToDataTable(); dt.TableName = "order"; var addRow = dt.NewRow(); addRow["id"] = 0; addRow["price"] = 1; addRow["Name"] = "a"; dt.Rows.Add(addRow); var x3 = db.Storageable(dt) .WhereColumns("id").ToStorage(); x3.AsInsertable.IgnoreColumns("id").ExecuteCommand(); x3.AsUpdateable.ExecuteCommand(); var x4 = db.Storageable(dt) .SplitDelete(it => Convert.ToInt32(it["id"]) > 0) .WhereColumns("id").ToStorage(); x4.AsDeleteable.ExecuteCommand(); Console.WriteLine(""); Console.WriteLine("#### Saveable End ####"); }