private async void Initialization(DBAction dBAction) { try { EnableGUIWait(); idLookupTable = new Hashtable(); if (dBAction.Equals(DBAction.StartFromScratch)) { await PrepareDB(); await SetDefaultTiers(); } else if (dBAction.Equals(DBAction.StartOver)) { await DeleteTieringInDB(); await SetDefaultTiers(); } await BuildOUStructure(); if (dBAction.Equals(DBAction.StartFromScratch) || dBAction.Equals(DBAction.StartOver)) { await SetDefaultTiersForImprohoundCreatedOUs(); } DisableGUIWait(); } catch (Exception err) { // Error MessageBox.Show(err.Message.ToString() + "\n\n" + err.StackTrace.ToString(), "Error", MessageBoxButton.OK, MessageBoxImage.Error); DisableGUIWait(); MainWindow.BackToConnectPage(); } }
public object AddOrUpdate(T obj, params object[] paras) { Type t = obj.GetType(); //NEED CACHE var sp = Spring; string TableName = sp.TableName, PKName = sp.PKName, Columns = sp.Columns; bool 自增编号 = sp.IdentityPK; object temp操作 = null; DBAction 插入数据 = DBAction.插入; if (paras != null && paras.Length > 0) { temp操作 = paras.FirstOrDefault(x => x.GetType().Equals(typeof(DBAction))); if (temp操作 != null) { 插入数据 = (DBAction)temp操作; } } PropertyInfo pk = t.GetProperty(PKName); if (temp操作 == null || 插入数据.Equals(DBAction.自动)) { if (pk != null && pk.CanWrite) { object vpk = pk.GetValue(obj, null); //var vpk = PropertyCallAdapterProvider<T>.GetInstance(PKName).InvokeGet(obj); if (vpk != null) { if (!(vpk.ToString().Equals("0"))) { 插入数据 = DBAction.更新; } else { 插入数据 = DBAction.插入; } } } } string sql = ""; Hashtable ht = new Hashtable(); Dictionary <string, string> _扩展叠加字段 = null; if (paras != null) { _扩展叠加字段 = (Dictionary <string, string>)paras.FirstOrDefault(x => x.GetType().Equals(typeof(Dictionary <string, string>))); } if (_扩展叠加字段 == null) { _扩展叠加字段 = new Dictionary <string, string>(); } var hashAppendKey = obj.GetHashCode().ToString(); foreach (PropertyInfo pi in t.GetProperties()) { string piName = pi.Name;//不做处理,注意大小写 object v = pi.GetValue(obj, null); try { if ((string.IsNullOrWhiteSpace(Columns) || Columns.IndexOf(piName.GetForkColumn()) != -1)) { bool 赋值 = true; if (v != null && v.GetType().Equals(typeof(DateTime))) { //对日期联系的做特殊处理 if (((DateTime)v).Year < 1753) { 赋值 = false; } } if (赋值) { if (piName.Equals(PKName, StringComparison.CurrentCultureIgnoreCase)) { //是主键 if (插入数据.Equals(DBAction.插入) && v != null && v.ToString() != "0") { //是插入,有值,OK sql += piName + ","; } } else { //OK sql += piName + ","; } var tmpt = pi.PropertyType; if (!tmpt.IsValueType && tmpt != typeof(string)) { v = JSONHelper.ToJSON(v); } ht[piName + hashAppendKey] = v; } } } catch (Exception ep) { Console.WriteLine(ep.ToString()); } } if (ht == null || ht.Count < 1) { return(null); } foreach (var xi in _扩展叠加字段) { var mi = xi.Key + hashAppendKey; if (ht.ContainsKey(mi)) { continue; //不允许更新已经被修改的值 } sql += xi.Key + ","; ht[mi] = xi.Value; } sql = sql.Trim(','); if (string.IsNullOrEmpty(sql)) { return(null); } if (插入数据.Equals(DBAction.插入)) { var preargs = string.Join(",", from xi in sql.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries) select "@" + xi.Trim() + obj.GetHashCode()); sql = String.Format(" insert into {0} ({1}) values ({2}) ", TableName, sql, preargs); } else { var preargs = string.Join(",", from xi in sql.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries) select xi.Trim() + "=@" + xi.Trim() + obj.GetHashCode()); sql = String.Format(" update {0} set {1} where {2}=@{2}{3}", TableName, preargs, PKName, obj.GetHashCode()); } bool 就地执行 = false; if (paras != null) { for (int i = 0; i < paras.Length; i++) { object o = paras[i]; if (null == o) { continue; } if (o.GetType().Equals(typeof(StringBuilder))) { StringBuilder sb = o as StringBuilder; if (i.Equals(0)) { sb.AppendFormat(" {0}", sql); } else { sb.Insert(0, sql); } sql = sb.ToString(); } else if (o.GetType().Equals(typeof(Hashtable))) { Hashtable newht = o as Hashtable; foreach (DictionaryEntry de in ht) { if (!newht.ContainsKey(de.Key)) { newht.Add(de.Key, de.Value); } } ht = newht; } else if (o.GetType().Equals(typeof(bool))) { bool 是否执行 = (bool)o; if (是否执行) { 就地执行 = true; } } } } if (就地执行) { bool 需返回值 = false; //直接执行 PropertyInfo pi = t.GetProperty(PKName); if (自增编号 && 插入数据.Equals(DBAction.插入)) { object defaultvalue = pi.GetValue(obj, null); if (defaultvalue == null || defaultvalue.ToString().Equals("0")) { 需返回值 = true; sql += " ; " + RunDB.GetAutoCols(PKName + obj.GetHashCode().ToString()); } } DataSet ds = null; if (需返回值) { ds = RunDB.GetDataSet(sql, ht, DbConnectionString); if (!Common.IsNull(ds)) { if (插入数据.Equals(DBAction.插入) && ds.Tables.Count > 0 && !Common.IsNull(ds.Tables[ds.Tables.Count - 1])) { if (ds.Tables[ds.Tables.Count - 1].Columns.Contains(PKName + obj.GetHashCode().ToString())) { if (pi != null && pi.CanWrite) { var od = ds.Tables[ds.Tables.Count - 1].Rows[0][PKName + obj.GetHashCode().ToString()]; var re_id_obj = od.ToString(); if (pi.PropertyType == typeof(int)) { pi.SetValue(obj, int.Parse(re_id_obj), null); } else if (pi.PropertyType == typeof(long)) { pi.SetValue(obj, long.Parse(re_id_obj), null); } else if (pi.PropertyType == typeof(ulong)) { pi.SetValue(obj, ulong.Parse(re_id_obj), null); } else if (pi.PropertyType == typeof(uint)) { pi.SetValue(obj, uint.Parse(re_id_obj), null); } else { pi.SetValue(obj, od, null); } } } } } } else { RunDB.Execute(sql, ht, DbConnectionString); } return(ds); } return(new ArrayList() { sql, ht }); }