示例#1
0
        public static List <T> SelectByWhere <T>(string sql) where T : ModelBase
        {
            List <T> ls = new List <T>();

            Type t = typeof(T);

            using (SqlDataReader sd = DBHelp.ExecuteDataReader(sql))
            {
                while (sd.Read()) //读取数据库里面的数据
                {
                    T t1 = Activator.CreateInstance <T>();
                    //得到这个类型的属性
                    PropertyInfo[] pp = t.GetProperties();
                    foreach (var item in pp)
                    {
                        if (sd[item.Name] != DBNull.Value)
                        {
                            //赋值
                            item.SetValue(t1, sd[item.Name]);
                        }
                    }
                    ls.Add(t1);
                }
            }



            return(ls);
        }
示例#2
0
        public static List <T> Selects <T>() where T : ModelBase
        {
            List <T> ls = new List <T>();

            //生成一段SQL语句
            Type   type = typeof(T);
            string sql  = "select * from " + type.Name;

            //泛型集合,在编译之前就对类型进行了严格的检查
            using (SqlDataReader sd = DBHelp.ExecuteDataReader(sql))
            {
                while (sd.Read()) //读取数据库里面的数据
                {
                    T t1 = Activator.CreateInstance <T>();
                    //得到这个类型的属性
                    PropertyInfo[] ps = type.GetProperties();
                    foreach (var item in ps)
                    {
                        if (sd[item.Name] != DBNull.Value)
                        {
                            //赋值
                            item.SetValue(t1, sd[item.Name]);
                        }
                    }
                    ls.Add(t1);
                    //Studens s = new Studens();
                    //s.classID =
                }
            }

            return(ls);
        }