예제 #1
0
 /// <summary>
 /// Reader 转 List
 /// </summary>
 /// <typeparam name="T">实体</typeparam>
 /// <param name="dr">数据</param>
 /// <returns>List 泛型</returns>
 public static List <T> ReaderToList <T>(IDataReader dr)
 {
     using (dr)
     {
         List <string> list1 = new List <string>(dr.FieldCount);
         for (int i = 0; i < dr.FieldCount; ++i)
         {
             list1.Add(dr.GetName(i).ToLower());
         }
         List <T> list2 = new List <T>();
         while (dr.Read())
         {
             T instance = Activator.CreateInstance <T>();
             foreach (PropertyInfo propertyInfo in instance.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty))
             {
                 if (list1.Contains(propertyInfo.Name.ToLower()) && !DataReaderConversion.IsNullOrDBNull(dr[propertyInfo.Name]))
                 {
                     propertyInfo.SetValue((object)instance, DataReaderConversion.HackType(dr[propertyInfo.Name], propertyInfo.PropertyType), (object[])null);
                 }
             }
             list2.Add(instance);
         }
         return(list2);
     }
 }
예제 #2
0
        /// <summary>
        /// Reader 转 Model
        /// </summary>
        /// <typeparam name="T">实体</typeparam>
        /// <param name="dr">数据</param>
        /// <returns>Model</returns>
        public static T ReaderToModel <T>(IDataReader dr)
        {
            T instance = Activator.CreateInstance <T>();

            while (dr.Read())
            {
                foreach (PropertyInfo propertyInfo in instance.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty))
                {
                    if (!DataReaderConversion.IsNullOrDBNull(dr[propertyInfo.Name]))
                    {
                        propertyInfo.SetValue((object)instance, DataReaderConversion.HackType(dr[propertyInfo.Name], propertyInfo.PropertyType), (object[])null);
                    }
                }
            }
            return(instance);
        }
예제 #3
0
        /// <summary>
        /// 查询数据分页 - 返回List<T> 有参
        /// </summary>
        /// <param name="sql">sql 语句</param>
        /// <param name="param">入参</param>
        /// <param name="orderField">排序</param>
        /// <param name="orderType">排序类型</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">每页显示数</param>
        /// <param name="count">返回总行数</param>
        /// <returns>List</returns>
        public static List <T> GetPageList <T>(string sql, DbParameter[] param, string orderField, string orderType, int pageIndex, int pageSize, ref int count)
        {
            StringBuilder stringBuilder = new StringBuilder();

            if (pageIndex == 0)
            {
                pageIndex = 1;
            }
            int    num1 = (pageIndex - 1) * pageSize;
            int    num2 = pageIndex * pageSize;
            string str  = string.IsNullOrEmpty(orderField) ? "Order By (select 0)" : "Order By " + orderField + " " + orderType;

            stringBuilder.Append("Select * From (Select ROW_NUMBER() Over (" + str + ")");
            stringBuilder.Append(" As rowNum, * From (" + (object)sql + ") As T ) As N Where rowNum > " + (string)(object)num1 + " And rowNum <= " + (string)(object)num2);
            count = Convert.ToInt32(DbHelper.ExecuteScalar(CommandType.Text, "Select Count(1) From (" + sql + ") As t", param));
            return(DataReaderConversion.ReaderToList <T>(DbHelper.ExecuteReader(CommandType.Text, stringBuilder.ToString(), param)));
        }