c# datatable如何转实体类

引用 https://www.cnblogs.com/H2921306656/p/6675327.html

然后做了小改动,转实体类时,判断不同类型的字段,然后自动转换成相关的字段类型

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace DealFile
{

    public class DatatableToEntity<T> where T : new()
    {
        /// <summary>
        /// 填充对象列表:用DataSet的第一个表填充实体类
        /// </summary>
        /// <param name="ds">DataSet</param>
        /// <returns></returns>
        public static List<T> FillModel(DataSet ds)
        {
            if (ds == null || ds.Tables[0] == null || ds.Tables[0].Rows.Count == 0)
            {
                return null;
            }
            else
            {
                return FillModel(ds.Tables[0]);
            }
        }

        // <summary>  
        /// 填充对象列表:用DataSet的第index个表填充实体类
        /// </summary>  
        public static List<T> FillModel(DataSet ds, int index)
        {
            if (ds == null || ds.Tables.Count <= index || ds.Tables[index].Rows.Count == 0)
            {
                return null;
            }
            else
            {
                return FillModel(ds.Tables[index]);
            }
        }

        /// <summary>  
        /// 填充对象列表:用DataTable填充实体类
        /// </summary>  
        public static List<T> FillModel(DataTable dt)
        {
            if (dt == null || dt.Rows.Count == 0)
            {
                return null;
            }
            List<T> modelList = new List<T>();
            foreach (DataRow dr in dt.Rows)
            {
                //T model = (T)Activator.CreateInstance(typeof(T));  
                T model = new T();
                for (int i = 0; i < dr.Table.Columns.Count; i++)
                {
                    PropertyInfo propertyInfo = model.GetType().GetProperty(dr.Table.Columns[i].ColumnName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);

                    if (propertyInfo != null && dr[i] != DBNull.Value)
                    {
                        string value = dr[i] == null ? "" : dr[i].ToString();
                        //string typestr = dr[i].GetType().Name;
                        //if(typestr.Equals("DateTime"))
                        //value = dr[i].ToString();
                        
                        Type type = propertyInfo.PropertyType;
                        if (type == typeof(Int32))
                        {
                            var convertType = Convert.ToInt32(value);
                            propertyInfo.SetValue(model, convertType, null);
                        }
                        else if (type == typeof(decimal))
                        {
                            propertyInfo.SetValue(model, Convert.ToDecimal(value), null);
                        }
                        else if (type == typeof(DateTime))
                        {
                            propertyInfo.SetValue(model, Convert.ToDateTime(value), null);
                        }
                        else if (type == typeof(string))
                        {
                            propertyInfo.SetValue(model, value, null);
                        }
                        
                    }

                }

                modelList.Add(model);
            }
            return modelList;
        }

        /// <summary>  
        /// 填充对象:用DataRow填充实体类
        /// </summary>  
        public static T FillModel(DataRow dr)
        {
            if (dr == null)
            {
                return default(T);
            }

            //T model = (T)Activator.CreateInstance(typeof(T));  
            T model = new T();

            for (int i = 0; i < dr.Table.Columns.Count; i++)
            {
                PropertyInfo propertyInfo = model.GetType().GetProperty(dr.Table.Columns[i].ColumnName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
                if (propertyInfo != null && dr[i] != DBNull.Value)
                {
                    string value = dr[i] == null ? "" : dr[i].ToString();
                    //string typestr = dr[i].GetType().Name;
                    //if(typestr.Equals("DateTime"))
                    //value = dr[i].ToString();
                    //propertyInfo.SetValue(model, value, null);
                    Type type = propertyInfo.PropertyType;
                    if (type == typeof(Int32))
                    {
                        var convertType = Convert.ToInt32(value);
                        propertyInfo.SetValue(model, convertType, null);
                    }
                    else if (type == typeof(decimal))
                    {
                        propertyInfo.SetValue(model, Convert.ToDecimal(value), null);
                    }
                    else if (type == typeof(DateTime))
                    {
                        propertyInfo.SetValue(model, Convert.ToDateTime(value), null);
                    }
                    else if (type == typeof(string))
                    {
                        propertyInfo.SetValue(model, value, null);
                    }
                }
            }
            return model;
        }

    }
}

 

转换代码是这个:上述代码中已添加

Type type = propertyInfo.PropertyType;
                        if (type == typeof(Int32))
                        {
                            var convertType = Convert.ToInt32(value);
                            propertyInfo.SetValue(model, convertType, null);
                        }
                        else if (type == typeof(decimal))
                        {
                            propertyInfo.SetValue(model, Convert.ToDecimal(value), null);
                        }
                        else if (type == typeof(DateTime))
                        {
                            propertyInfo.SetValue(model, Convert.ToDateTime(value), null);
                        }
                        else if (type == typeof(string))
                        {
                            propertyInfo.SetValue(model, value, null);
                        }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值