MySqlQueryList

//辅助查询列表,或实例
    public class MySqlQueryList
    {
        #region List<T> ToList<T>(string sql, params DbParameter[] parameters) where T : new()
        /// <summary>
        /// 返回一个list
        /// </summary>
        /// <typeparam name="T">实体模型</typeparam>
        /// <param name="sql">sql语句</param>
        /// <param name="parameters">参数</param>
        /// <returns></returns>
        public static List<T> ToList<T>(string sql, params DbParameter[] parameters) where T : new()
        {
            using (var read = MysqlHelp.ExecuteReader(sql, parameters))
            {
                List<T> list = null;
                var type = typeof(T);
                if (read.HasRows)
                {

                    list = new List<T>();
                }
                while (read.Read())
                {
                    T t = new T();
                    foreach (PropertyInfo item in type.GetProperties())
                    {
                        for (int i = 0; i < read.FieldCount; i++)
                        {
                            //属性名与查询出来的列名比较
                            if (item.Name.ToLower() != read.GetName(i).ToLower()) continue;
                            var value = read[i];
                            if (value != DBNull.Value)
                            {
                                item.SetValue(t, value, null);
                            }
                            break;
                        }
                    }
                    //将创建的对象添加到集合中
                    list.Add(t);
                }
                return list;
            }
        }
        #endregion

        #region T FirstOrDefault<T>(string sql, params DbParameter[] parameters)
        /// <summary>
        /// 返回一个实体模型
        /// </summary>
        /// <typeparam name="T">实体模型</typeparam>
        /// <param name="sql">sql语句</param>
        /// <param name="parameters">参数</param>
        /// <returns></returns>
        public static T FirstOrDefault<T>(string sql, params DbParameter[] parameters) where T : class,new()
        {
            using (var read = MysqlHelp.ExecuteReader(sql, parameters))
            {

                Type type = typeof(T);

                if (!read.Read()) return null;
                T t = new T();
                foreach (PropertyInfo item in type.GetProperties())
                {
                    for (int i = 0; i < read.FieldCount; i++)
                    {
                        //属性名与查询出来的列名比较
                        if (item.Name.ToLower() != read.GetName(i).ToLower()) continue;
                        object value = read[i];
                        if (value != DBNull.Value)
                        {
                            item.SetValue(t, value, null);
                        }
                        break;
                    }

                }
                return t;
            }
        }
        #endregion

        #region 分页查询信息 + List<T> ToListAsPager<T>(string sql,List<DbParameter> parameters, out int pageCount) where T : class,new()
        /// <summary>
        /// 分页查询信息GetTByPager
        /// </summary>
        /// <typeparam name="T">实体对象</typeparam>
        /// <param name="parameters">参数列表</param>
        /// <param name="pageCount">总页数</param>
        /// <param name="sql">sql语句</param>
        /// <returns>IList</returns>
        public static List<T> ToListAsPager<T>(string sql, List<DbParameter> parameters, out int pageCount) where T : class,new()
        {
            //存储过程名称
            List<T> contents = null;
            parameters[parameters.Count - 1].Direction = System.Data.ParameterDirection.Output;
            using (var reader = MysqlHelp.ExecuteReader(sql, parameters.ToArray()))
            {
                if (reader.HasRows) contents = new List<T>();
                var type = typeof(T);
                while (reader.Read())
                {
                    var t = new T();
                    //通用反射获取类的所有属性
                    foreach (var p in type.GetProperties())
                    {
                        //循环遍历reader读取的所有字段
                        for (var i = 0; i < reader.FieldCount; i++)
                        {
                            //属性名与查询出来的列名比较
                            if (p.Name.ToLower() != reader.GetName(i).ToLower()) continue;
                            var value = reader[i];
                            //如果该value不数据库的NULL,则保存到实体对象中
                            if (DBNull.Value != value)
                            {
                                p.SetValue(t, value, null);
                            }
                            break;
                        }
                    }
                    if (contents != null) contents.Add(t);
                }
                //执行 SqlHelp.ExecuteNonQuery(cmdText, parameters); 返回总页数 pageCount
                SQLhelp.ExcuteNonQuery(sql, parameters.ToArray());
                //返回存储过程中的输出值
                pageCount = Convert.ToInt32(parameters[parameters.Count - 1].Value);
            }
            return contents;
        }
        #endregion
}



      //使用DataTable,查询数据
        DataTable dt = MysqlHelp.ExecuteTable(sql, new SqlParameter("@sDelFlag", delflag));
        List<TbStudentA> list = new List<TbStudentA>();

         if (dt.Rows.Count > 0)
         {
             foreach (DataRow item in dt.Rows)
             {
             TbStudentA ts = new TbStudentA();
             td.ClassId = Convert.ToInt32(item["列名"]);
             list.Add(ts);
             }
         }
return list;

 

posted @ 2015-03-25 14:32  逍遥帝君  阅读(530)  评论(0编辑  收藏  举报