public T[] List <T>(DbDataReader dr, List <string> columns = null) { List <T> list = new List <T>(); while (dr.Read()) { T md = Activator.CreateInstance <T>(); lib.Class.Reflection rfc = new lib.Class.Reflection(md); string[] cols = rfc.GetProperties(); for (int i = 0; i < cols.Length; i++) { int ordinal = -1; if (columns == null) { ordinal = dr.GetOrdinal(cols[i]); } else { ordinal = columns.IndexOf(cols[i].ToUpper()); } if (ordinal >= 0) { string columnName = cols[i]; object columnValue = dr.GetValue(ordinal); rfc.SetAttibute(columnName, columnValue); } } list.Add(md); DbBase.OnLineCounter(); } return(list.ToArray()); }
public void FillObject(DataRow dr, object Obj) { lib.Class.Reflection oa = new lib.Class.Reflection(Obj); for (int i = 0; i < dr.Table.Columns.Count; i++) { string ColName = dr.Table.Columns[i].ColumnName; oa.SetAttibute(ColName, dr[ColName].ToString()); } }
public string GetDbValue(lib.Class.Reflection r, System.Reflection.PropertyInfo pi, bool IsSelect) { string value = ""; Type tp = pi.PropertyType; lib.Class.Conversion Obj = new lib.Class.Conversion(r.GetPropertyValue(pi)); if (tp == typeof(string)) { if (IsSelect) { value = string.Format("{0}", DbQuoted("%" + Obj.ToString() + "%")); } else { value = string.Format("{0}", DbQuoted(Obj.ToString())); } } else if (tp == typeof(DateTime)) { DateTime dt = Obj.ToDateTime(); if (dt < lib.Class.Utils.StartGregorianCalendar()) { dt = DateTime.MinValue; } if (dt == DateTime.MinValue) { value = "NULL"; } else { value = string.Format("'{0}'", dt.ToString("yyyy-MM-dd HH:mm:ss")); } } else if (tp == typeof(bool)) { value = Obj.ToBool() ? "1" : "0"; } else if (tp == typeof(decimal) || tp == typeof(double) || tp == typeof(float)) { value = Obj.ToDecimal().ToString("0.00").Replace(",", "."); } else if (tp == typeof(int) || tp == typeof(long)) { value = Obj.ToString(); } else { value = Obj.ToString(); } return(value); }
private System.Reflection.PropertyInfo[] GetValidProperties(lib.Class.Reflection r) { List <System.Reflection.PropertyInfo> l = new List <System.Reflection.PropertyInfo>(); for (int i = 0; i < r.Properties.Length; i++) { KeyTypeAttributeState state = GetKeyTypeAttributeState(r.Properties[i]); if (!r.IsDefaultValue(r.Properties[i]) && !state.IsQueryField) { l.Add(r.Properties[i]); } } return(l.ToArray()); }
public void Insert <T>(T entity, DbTransaction Transaction = null) { lib.Class.Reflection r = new lib.Class.Reflection(entity); if (r.Properties.Length == 0) { return; } string db_fields = ""; for (int i = 0; i < r.Properties.Length; i++) { KeyTypeAttributeState state = GetKeyTypeAttributeState(r.Properties[i]); bool IsDefaultValue = r.IsDefaultValue(r.Properties[i]); if (state.IsQueryField || (IsDefaultValue && state.IsOmitIfIsNull)) { continue; } if (!state.IsPrimaryKey && !(state.IsForeignKey && IsDefaultValue)) { db_fields += (string.IsNullOrEmpty(db_fields) ? "" : ",") + r.Properties[i].Name; } } string db_values = ""; for (int i = 0; i < r.Properties.Length; i++) { KeyTypeAttributeState state = GetKeyTypeAttributeState(r.Properties[i]); bool IsDefaultValue = r.IsDefaultValue(r.Properties[i]); if (state.IsQueryField || (IsDefaultValue && state.IsOmitIfIsNull)) { continue; } if (!state.IsPrimaryKey && !(state.IsForeignKey && IsDefaultValue)) { db_values += (string.IsNullOrEmpty(db_values) ? "" : ",") + GetDbValue(r, r.Properties[i], false); } } string sql = string.Format("{0} INSERT INTO {1} ({2}) VALUES ({3});", DbBase.DbSetDateFormat, typeof(T).Name, db_fields, db_values); DbBase.DbExecute(sql, Transaction); }
public void Assign(object o) { lib.Class.Reflection foParam = new lib.Class.Reflection(o); string[] flds = foParam.GetFields(); for (int i = 0; i < flds.Length; i++) { foThis.SetField(flds[i], foParam.GetFieldValue(flds[i])); } string[] prps = foParam.GetProperties(); for (int i = 0; i < prps.Length; i++) { foThis.SetProperty(prps[i], foParam.GetPropertyValue(prps[i])); } }
public void Update <T>(T entity, T Conditions, DbTransaction Transaction = null) { lib.Class.Reflection r = new lib.Class.Reflection(entity); if (r.Properties.Length == 0) { return; } System.Reflection.PropertyInfo[] PropsCondition = null; if (Conditions != null) { PropsCondition = GetValidProperties(new lib.Class.Reflection(Conditions)); } string db_fields_values = ""; for (int i = 0; i < r.Properties.Length; i++) { KeyTypeAttributeState state = GetKeyTypeAttributeState(r.Properties[i]); bool IsDefaultValue = r.IsDefaultValue(r.Properties[i]); if (state.IsQueryField || state.IsPrimaryKey || (IsDefaultValue && state.IsOmitIfIsNull)) { continue; } if (state.IsForeignKey && IsDefaultValue) { db_fields_values += (string.IsNullOrEmpty(db_fields_values) ? "" : ",") + string.Format("{0}=NULL", r.Properties[i].Name); } else { db_fields_values += (string.IsNullOrEmpty(db_fields_values) ? "" : ",") + string.Format("{0}={1}", r.Properties[i].Name, GetDbValue(r, r.Properties[i], false)); } } DbBase.DbExecute( string.Format("{0} UPDATE {1} SET {2} {3}", DbBase.DbSetDateFormat, typeof(T).Name, db_fields_values, Condition(Conditions)), Transaction); }
protected string Condition <T>(T values) { if (values == null) { return(""); } lib.Class.Reflection r = new lib.Class.Reflection(values); string where = ""; System.Reflection.PropertyInfo[] props = GetValidProperties(r); for (int i = 0; i < props.Length; i++) { Type tp = props[i].PropertyType; string signal = (tp == typeof(string) ? "LIKE" : "="); string value = GetDbValue(r, props[i], true); where += string.Format("{0} {1} {2}", props[i].Name, signal, value) + (i == (props.Length - 1) ? " " : " and "); } return(string.Format("WHERE {0}", where)); }
public DefaultEntity() { foThis = new lib.Class.Reflection(this); this.Clear(); }