这个是抽象类Entity的的方法:泛型E 约束为 Entity<E>
  /// <summary>
         /// 给实体类搜索数据库用
         /// </summary>
         /// <param name="columnName">要搜索列名</param>
         /// <param name="columnValue">要搜索列值</param>
         /// <returns></returns>
        public static List<E> Select(String columnName, object columnValue)
         {
            //查询到数据的列表
             List<E> rete = new List<E>();             Type type = typeof(E);         //获取对象的包含有column的属性,既对应的数据库列名
         List<PropertyInfo> pi = new List<PropertyInfo>(
             type.GetProperties().Where<PropertyInfo>
             (
             P => P.GetCustomAttributes(typeof(ColumnAttribute), true).Length > 0
             )
         );         //将列名累加为字符串
         StringBuilder sql = new StringBuilder();         foreach (var p in pi)
         {
             sql.AppendFormat("{0},", p.Name.ToString());
         }             //操作数据库
             using(System.Data.SqlClient.SqlConnection connection = ConnectionHelper.CreateConnection())
             {
                 System.Data.SqlClient.SqlCommand command = connection.CreateCommand();
                 connection.Open();
                 command.CommandText = String.Format("SELECT {0} FROM {1} WHERE {2}=@columnValue",
                     sql.Remove(sql.Length-1,1),
                     (type.GetCustomAttributes(typeof(TabelAttribute), true)[0] as TabelAttribute).TableName,
                     columnName);                 command.Parameters.AddWithValue("@columnValue", columnValue);
                 using(System.Data.SqlClient.SqlDataReader reader = command.ExecuteReader())
                 {
                     int i = 0;
                     E ne = new E();
                     while (reader.Read())
                     {
                         //要把reader去读到的值赋值到实体类里去,该怎么做 
                     }
                     rete.Add(ne);
                 }
                 return rete;        
             }
         } 
 User是这个类的一个继承,使用的方法是:User.Select("ID", 2);//意思是要检查所有ID列值是2的记录,返回值是所有检索到的记录生成的user实例的列表

解决方案 »

  1.   

    以前不用泛型的话很简单就能用this.ID=reaerd["ID"];
    this.Passwrod["Password"];
    现在用来泛型Entity<E>后就没有办法E.ID=reaerd["ID"];
    E.Passwrod["Password"];
    ╮(╯▽╰)╭小弟学艺不精,各位高手救下,拜谢
      

  2.   

                int i = 0;
                E ne = new E();
                while (reader.Read())
                {
                    ne = (E)rete[i];
                    en.ID = reader["ID"].ToString();
                    en.Passwrod = reader["Passwrod"].ToString();
                    i++;
                }
      

  3.   


     foreach (var p in pi)
             {
                string colname = p.GetCustomAttributes(typeof(ColumnAttribute), true);
                if (colname.Length > 0)
                {
                    p.SetValue(ne, reader[colname], null);
                }
             }
      

  4.   


    while (reader.Read())
            {
                E ne = new E();
                pi = ne.GetType().GetProperties();
                foreach (var p in pi)
                {
                    string colname = p.GetCustomAttributes(typeof(ColumnAttribute), true);
                    p.SetValue(ne, reader[colname], null);
                }
                rete.Add(ne);
            }
    没调试,,你看看行不。。
      

  5.   


    谢谢你帮忙,不过好像不行啊,E是泛型 不能直接点出实例类的属性的啊,password,ID这些是E不拥有的属性
      

  6.   

    稍微改下后可以了 
                         while (reader.Read())
                         {
                             E ne = new E();
                             foreach (var p in pi)
                             {
                                p.SetValue(ne, reader[p.Name], null);
                             }
                             rete.Add(ne);
                         }
    使用时候这样写:           List<User> us =  User.Select("Password", "123");