/// <summary> /// 获取数据表中某列的值的数组(不重复) /// </summary> /// <param name="dt"></param> /// <param name="fieldName"></param> /// <returns></returns> private static string[] GetColDistinct(this MDataTable dt, string fieldName) { var tmpDt = new MDataTable(); var d = dt.GetColumnItems <string>(fieldName); var a = d.Distinct <string>(); return(a.ToArray()); }
public virtual void Delete() { bool isIgnoreDeleteField = false; #if DEBUG isIgnoreDeleteField = UserAuth.IsSuperAdmin; #endif string ids = GetID; string[] values = null; string where = string.Empty; string parentField = Query <string>("parentField"); string idField = Query <string>("idField"); MDataTable dt = null; using (MAction action = new MAction(TableName)) { action.BeginTransation(); delChild: //删除子节点循环处。 //dg.foreignKeys="TableA.ColumnNameA,TableB.ColumnNameB"; string foreignKeys = Query <string>("foreignKeys"); bool result = true; if (!string.IsNullOrEmpty(foreignKeys))//增加了外键删除 { string[] items = foreignKeys.Split(','); foreach (string item in items) { string[] kv = item.Split('.'); if (kv.Length == 2) { action.ResetTable(kv[0]); result = action.Delete(GetWhereIn(kv[1], null, values), isIgnoreDeleteField); if (!result) { break; } } } if (result) { action.ResetTable(TableName); } } if (result) { //第一次,需要将id转为idField指定的上级关联字段。 string pkName = action.Data.Columns.FirstPrimary.ColumnName; if (string.IsNullOrEmpty(idField)) { idField = pkName; } where = values == null ? ids : GetWhereIn(idField, null, values); if (values == null && !string.IsNullOrEmpty(idField) && pkName.ToLower() != idField.ToLower()) { action.SetSelectColumns(idField); dt = action.Select(where); if (dt.Rows.Count > 0) { values = dt.GetColumnItems <string>(0).ToArray(); } } result = action.Delete(where, isIgnoreDeleteField); } if (!result) { action.RollBack(); } else if (!string.IsNullOrEmpty(parentField) && !string.IsNullOrEmpty(idField)) { action.SetSelectColumns(idField); dt = action.Select(GetWhereIn(parentField, null, values)); if (dt.Rows.Count > 0) { values = dt.GetColumnItems <string>(0).ToArray(); goto delChild; } } action.EndTransation(); if (result) { SetSuccess(LangConst.DeleteSuccess); } else { if (AppConfig.Debug.OpenDebugInfo) { Log.WriteLogToTxt("Delete(): " + action.DebugInfo); } SetError(LangConst.DeleteError, action.DebugInfo); } } }
private static string CreateSql(MDataTable dataSrc, MDataTable tableFieldShtTitleCompareSrc) { if (dataSrc == null) { return(null); } const string idCardFieldName = "PersonalIdCard"; var fieldCnNames = tableFieldShtTitleCompareSrc.GetColumnItems <string>(2); var sqls = new List <string>(); var fieldNames = new List <string>(); var values = new List <string>(); var idCardValue = ""; foreach (var row in dataSrc.Rows) { if (row[0] == null || row[0].Value.IsNullOrEmpty()) { continue; } var prevTableName = ""; foreach (var cell in row) { var index = fieldCnNames.IndexOf(cell.ColumnName); if (index < 0) { continue; } if (cell.ColumnName == "身份证号") { idCardValue = cell.Value.ToString(); } //fieldCnNames[index] = ""; var currTableName = tableFieldShtTitleCompareSrc.Rows[index]["TableName"].Value.ToString(); if (prevTableName != "" && currTableName != prevTableName) { if (fieldNames.IndexOf(idCardFieldName) < 0 && fieldNames.IndexOf("IdCard") < 0) { fieldNames.Add(idCardFieldName); values.Add(idCardValue); } ToSql(sqls, fieldNames, values, prevTableName); fieldNames = new List <string>(); values = new List <string>(); } prevTableName = currTableName; fieldNames.Add(tableFieldShtTitleCompareSrc.Rows[index]["EnName"].Value.ToString()); values.Add(cell.Value == null ? "isNullValue" : cell.Value.ToString()); } if (fieldNames.IndexOf(idCardFieldName) < 0 && fieldNames.IndexOf("IdCard") < 0 && prevTableName != "Institutions") { fieldNames.Add(idCardFieldName); values.Add(idCardValue); } ToSql(sqls, fieldNames, values, prevTableName); fieldNames = new List <string>(); values = new List <string>(); } return(sqls.Count > 0 ? sqls.ToArray().Join(" ") : null); }