日期:2014-05-18 浏览次数:20477 次
connText = "select * from " + tableName + " where " + f; SqlDataReader datar = SQLServer.SQLServer.getList(connText, parms); IList<T> listT = new List<T>(); while (datar.Read()) { T tmodel = new T(); int k=0; foreach (PropertyInfo pro in pros) { pro.SetValue(listT, datar.GetSqlValue(k), null); k++; } listT.Add(tmodel); }
public static IList<T> ToList<T>(SqlDataReader dr) where T:new() { List<T> list = new List<T>(); var properties = TypeDescriptor.GetProperties(typeof(T)); var fields = Enumerable.Range(0, dr.FieldCount-1).Select(i=> dr.GetName(i)).Where(name => properties[name] != null); while (dr.Read()) { T o = new T(); fields.ToList().ForEach(field => { properties[field].SetValue(o, dr[field] == DBNull.Value ? null : dr[field]); }); list.Add(o); } return list; }