public static List <T> MapModelCollection <T>(this IDataReader dr) where T : new() { Type business = typeof(T); List <T> entities = new List <T>(); Hashtable hashtable = new Hashtable(); PropertyInfo[] properties = business.GetProperties(); foreach (PropertyInfo info in properties) { hashtable[info.Name.ToUpper()] = info; } while (dr.Read()) { T item = new T(); for (int i = 0; i < dr.FieldCount; i++) { PropertyInfo info = (PropertyInfo)hashtable[dr.GetName(i).ToUpper()]; if (info != null && info.CanWrite) { Type infoField = info.PropertyType; if (infoField.FullName.ToLower() == "system.string") { info.SetValue(item, SanitaizeStrings.ToView((string)dr.GetValue(i)), null); } else { info.SetValue(item, dr.GetValue(i), null); } } } entities.Add(item); } return(entities); }
protected override string InsertQuery(object model) { string query = QueryManager.GetInstance().GetQuery((Model)model, QueryType.Create); Type info = model.GetType(); PropertyInfo[] properties = info.GetProperties(); foreach (PropertyInfo prop in properties) { if (prop.Name != QueryManager.ID) { Type pt = prop.PropertyType; if (pt.FullName.ToLower() == QueryManager.SSTRING) { query += "'" + SanitaizeStrings.ToDb((string)prop.GetValue(model)) + "',"; } else if (pt.FullName.ToLower() == QueryManager.SDATE) { query += "'" + prop.GetValue(model).ToString().Replace("/", "-") + "',"; } else { query += prop.GetValue(model) + ","; } } } query = query.Substring(0, query.Length - 1) + ");"; return(query); }
protected override string UpdateQuery(object model) { string query = QueryManager.GetInstance().GetQuery((Model)model, QueryType.Update); Type info = model.GetType(); PropertyInfo[] properties = info.GetProperties(); Hashtable id = new Hashtable(); foreach (PropertyInfo prop in properties) { ///TODO Coger primary key del GetValues if (prop.Name != QueryManager.ID) { Type pt = prop.PropertyType; if (pt.FullName.ToLower() == QueryManager.SSTRING) { query += prop.Name + "=" + "'" + SanitaizeStrings.ToDb((string)prop.GetValue(model)) + "',"; } else if (pt.FullName.ToLower() == QueryManager.SDATE) { query += prop.Name + "=" + "'" + prop.GetValue(model) + "',"; } else { query += prop.Name + "=" + prop.GetValue(model) + ","; } } else { ///TODO Coger la primary key de GetValues id[QueryManager.ID] = prop.GetValue(model) + ""; } } ///TODO Coger primary keys del GetValues query = query.Substring(0, query.Length - 1) + " WHERE " + QueryManager.ID + " = " + id[QueryManager.ID] + ";"; return(query); }