将DataReader读取到List中

来源:互联网 发布:gs域名 编辑:程序博客网 时间:2024/05/22 06:56
   public class CBO<T>    {        public static List<T> FillList(IDataReader dr,ref int count)        {            List<T> list = new List<T>();            T t = (T)Activator.CreateInstance(typeof(T));                                    Type tt=typeof(T);            //对象属性 个数            var pc = tt.GetProperties().Count();            while (dr.Read())            {                for (int i = 0; i <pc; i++)                {                                       if (dr[tt.GetProperties()[i].Name] == null)                        continue;                    tt.GetProperty(tt.GetProperties()[i].Name).SetValue(t, dr[tt.GetProperties()[i].Name], null);                }                list.Add(t);            }            if (dr.NextResult())            {                if (dr.Read())                {                    count = dr.GetInt32(0);                }            }            return list;        }        public static List<T> FillList(IDataReader dr, ref int count,Map<T> map)        {            return FillList(dr,ref count);        }    }