public List <FieldMap <T, K> > GetAllFieldMaps() { List <FieldMap <T, K> > elements = new List <FieldMap <T, K> >(); string sql = "select * from TF_FieldMap"; DataTable dt = sqlHelper.Query(sql); if (dt != null && dt.Rows.Count > 0) { for (int i = 0; i < dt.Rows.Count; i++) { FieldMap <T, K> element = new FieldMap <T, K>(); element.ID = Convert.ToInt32(dt.Rows[i]["ID"]); element.Name = dt.Rows[i]["Name"].ToString(); element.Map = (byte[])dt.Rows[i]["Map"]; elements.Add(element); } } return(elements); }
public int AddFieldMap(FieldMap <T, K> element) { string sql = "insert into TF_FieldMap (Name, Map) values (@Name, @Map); select SCOPE_IDENTITY()"; SqlParameter[] para = new SqlParameter[] { new SqlParameter("@Name", element.Name), new SqlParameter("@Map", element.Map) }; object obj = sqlHelper.ExecuteSqlReturn(sql, false, para); int R; if (obj != null && obj != DBNull.Value && int.TryParse(obj.ToString(), out R)) { return(R); } else { return(0); } }
public static R GetData <R>(DataRow row, FieldMap <string, string> map, R element, Type t) where R : class { if (row != null && map != null) { if (element != null && t != null) { PropertyInfo[] pis = t.GetProperties(); for (int i = 0; i < pis.Length; i++) { PropertyInfo pi = pis[i]; if (pi.CanWrite && map.Keys.Contains(pi.Name)) { try { t.GetProperty(pi.Name).SetValue(element, row[map[pi.Name]], null); } catch { try { string colName = map[pi.Name]; object cell = row[colName]; if (cell != null) { if (colName.Contains("性别")) { t.GetProperty(pi.Name).SetValue(element, (性别)Enum.Parse(typeof(性别), cell.ToString()), null); } else if (colName.Contains("类型") || colName.Contains("种类") || colName.Contains("卡种")) { CardType ct = CardTypeLogic.GetInstance().GetCardTypeByName(cell.ToString()); t.GetProperty(pi.Name).SetValue(element, ct, null); } else if (colName.Contains("状态")) { StaffCondition sc = StaffConditionLogic.GetInstance().GetStaffConditionByName(cell.ToString()); t.GetProperty(pi.Name).SetValue(element, sc, null); } else if (colName.Contains("开始日") || colName.Contains("到期日") || colName.Contains("生日") || colName.Contains("日期") || colName.Contains("时间")) { DateTime dt = DateTime.MinValue; DateTime T; if (DateTime.TryParse(cell.ToString(), out T)) { dt = T; } t.GetProperty(pi.Name).SetValue(element, dt, null); } } } catch (Exception e) { throw e; } } } } } } return(element); }
public static bool ImportData(string elementType, bool clearOldData, DataTable data, FieldMap <string, string> map, ImportDataProcessHandler process) { if (data == null && data.Rows.Count == 0) { return(false); } if (map.Count == 0) { return(false); } try { Assembly ass = null; Type t = null; string typeName = null; //清空数据库中的原有数据 switch (elementType) { case "会员": ass = Assembly.Load("Model"); t = ass.GetType("TopFashion.Member", false, true); typeName = t.FullName; if (clearOldData) { MemberLogic.GetInstance().ClearMembers(); } break; case "员工": ass = Assembly.Load("Model"); t = ass.GetType("TopFashion.Staff", false, true); typeName = t.FullName; if (clearOldData) { StaffLogic.GetInstance().ClearStaffs(); } break; default: break; } if (!string.IsNullOrEmpty(typeName)) { for (int i = 0; i < data.Rows.Count; i++) { DataRow row = data.Rows[i];//FormatData(data.Rows[i]); //保存数据到数据库 switch (elementType) { case "会员": Member elementM = ass.CreateInstance(typeName, true) as Member; Member member = GetData <Member>(row, map, elementM, t); MemberLogic.GetInstance().AddMember(member); break; case "员工": Staff elementS = ass.CreateInstance(typeName, true) as Staff; Staff staff = GetData <Staff>(row, map, elementS, t); StaffLogic.GetInstance().AddStaff(staff); break; default: break; } //反馈进度给外部程序 if (process != null) { process(i + 1); } } } return(true); } catch (Exception) { return(false); } }
private void ImportData(DataTable data, FieldMap <string, string> map, bool clearOldData = true) { ImportDataProcessHandler process = new ImportDataProcessHandler(ImportProcess); bool f = Common.ImportData(comboBox2.Text, clearOldData, data, map, process); }