/// <summary> /// 根据需要得实体类信息 /// </summary> /// <typeparam name="T">需要一个对象有一个无参数的实例化方法</typeparam> /// <param name="dr">table数据源</param> /// <returns>返回整理好了集合</returns> private static IList<T> GetEntityList<T>(IDataReader dr) where T :new() { IList<T> entityList = new List<T>(); int fieldCount = -1; while (dr.Read()) { if (-1 == fieldCount) fieldCount = dr.FieldCount; // 得到实体类对象 T t = (T)Activator.CreateInstance(typeof(T)); for (int i = 0; i < fieldCount; i++) { PropertyInfo prop = t.GetType().GetProperty(dr.GetName(i), BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public); if (null != prop) { // 为了能用在默认为null的值上 // 如 DateTime? tt = null; if ( null == dr[i] || Convert.IsDBNull(dr[i])) prop.SetValue(t, null, null); else prop.SetValue(t, dr[i], null); } } entityList.Add(t); } dr.Close(); return entityList; } 使用方法如下: IList<Entity> ent = null; using (SqlConnection conn = new SqlConnection("server=###;database=###;user=###;password=###;")) { conn.Open(); using (SqlCommand cmd = new SqlCommand("select name,create_date,modify_date from sys.procedures", conn)) { using (IDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)) { ent = GetEntityList<Entity>(dr); } conn.Close(); } } 数据实体 // 数据实体 public class Entity { private string _name; public string Name { get { return _name; } set { _name = value; } } private DateTime _createdate; public DateTime Create_date { get { return _createdate; } set { _createdate = value; } } private DateTime _modifydate; public DateTime Modify_date { get { return _modifydate; } set { _modifydate = value; } } }