sqldatareader 转实体类 2

using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;

namespace Aqioo.Modules.Consult.Extensions
{
    /// <summary>
    /// Summary description for ConvertEntity1
    /// </summary>
    public static class IDataReaderExt
    {
        public static T ReaderToModel<T>(this IDataReader dr)
        {
            //  try
            //  {
            using (dr)
            {
                if (dr.Read())
                {
                    Type modelType = typeof(T);
                    int count = dr.FieldCount;
                    T model = Activator.CreateInstance<T>();
                    for (int i = 0; i < count; i++)
                    {
                        if (!IsNullOrDBNull(dr[i]))
                        {
                            PropertyInfo pi = modelType.GetProperty(GetPropertyName(dr.GetName(i)), BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
                            if (pi != null)
                            {
                                pi.SetValue(model, HackType(dr[i], pi.PropertyType), null);
                            }
                        }
                    }
                    return model;
                }
            }
            return default(T);
            //   }
            //  catch (Exception ex)
            //  {
            //      return default(T);
            //   }
        }

        public static IList<T> ReaderToList<T>(this IDataReader dr)
        {
            using (dr)
            {
                List<T> list = new List<T>();
                Type modelType = typeof(T);
                int count = dr.FieldCount;
                while (dr.Read())
                {
                    T model = Activator.CreateInstance<T>();

                    for (int i = 0; i < count; i++)
                    {
                        if (!IsNullOrDBNull(dr[i]))
                        {
                            PropertyInfo pi = modelType.GetProperty(GetPropertyName(dr.GetName(i)), BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
                            if (pi != null)
                            {
                                pi.SetValue(model, HackType(dr[i], pi.PropertyType), null);
                            }
                        }
                    }
                    list.Add(model);
                }
                return list;
            }
        }
        //这个类对可空类型进行判断转换,要不然会报错  
        private static object HackType(object value, Type conversionType)
        {
            if (conversionType.IsGenericType && conversionType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
            {
                if (value == null)
                    return null;

                System.ComponentModel.NullableConverter nullableConverter = new System.ComponentModel.NullableConverter(conversionType);
                conversionType = nullableConverter.UnderlyingType;
            }
            return Convert.ChangeType(value, conversionType);

        }

 

        private static bool IsNullOrDBNull(object obj)
        {
            return (obj == null || (obj is DBNull)) ? true : false;
        }

 

        //取得DB的列对应bean的属性名  

        private static string GetPropertyName(string column)
        {

            column = column.ToLower();

            string[] narr = column.Split('_');

            column = "";

            for (int i = 0; i < narr.Length; i++)
            {

                if (narr[i].Length > 1)
                {

                    column += narr[i].Substring(0, 1).ToUpper() + narr[i].Substring(1);

                }

                else
                {

                    column += narr[i].Substring(0, 1).ToUpper();

                }

            }

            return column;

        }


    }
}

转载于:https://www.cnblogs.com/jianshao810/archive/2010/06/09/1755066.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值