DataTable 转成 实体

<span style="font-family: Arial, Helvetica, sans-serif;">using System;</span>
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Reflection;

namespace ClassLibrary1
{
    public static class DataTableHelper
    {
        public static T FillEntityWithDataRow<T>(DataRow drInfo) where T : class,new()
        {
            T entity = new T();
            Type type = entity.GetType();
            PropertyInfo[] props = type.GetProperties();
            foreach (PropertyInfo prop in props)
            {
                if (drInfo.Table.Columns[prop.Name] != null)
                {
                    string drValue = drInfo[prop.Name] == DBNull.Value ? "" : drInfo[prop.Name].ToString();
                    switch (prop.PropertyType.Name)
                    {
                        #region 基本属性
                        case "DateTime":
                            DateTime dtValue;
                            if (DateTime.TryParse(drValue, out dtValue))
                            {
                                prop.SetValue(entity, dtValue, null);
                            }
                            break;
                        case "Boolean":
                            bool bValue = false;
                            if (bool.TryParse(drValue, out bValue))
                            {
                                prop.SetValue(entity, bValue, null);
                            }
                            break;
                        case "Int32":
                            int iValue = 0;
                            if (int.TryParse(drValue, out iValue))
                            {
                                prop.SetValue(entity, iValue, null);
                            }
                            break;
                        case "Int64":
                            long lValue = 0;
                            if (long.TryParse(drValue, out lValue))
                            {
                                prop.SetValue(entity, lValue, null);
                            }
                            break;
                        case "Double":
                            double dValue = 0;
                            if (double.TryParse(drValue, out dValue))
                            {
                                prop.SetValue(entity, dValue, null);
                            }
                            break;
                        case "Single":
                            float fValue = 0;
                            if (Single.TryParse(drValue, out fValue))
                            {
                                prop.SetValue(entity, fValue, null);
                            }
                            break;
                        case "String":
                            prop.SetValue(entity, drValue, null);
                            break;
                        #endregion
                    }
                }
            }
            return entity;
        }

        public static List<T> FillListWithDataTable<T>(DataTable dtInfo) where T: class,new()
        {
            List<T> list = new List<T>();
            foreach (DataRow drinfo in dtInfo.Rows)
            {
                list.Add(FillEntityWithDataRow<T>(drinfo));
            }
            return list;
        }
    }
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值