码迷,mamicode.com
首页 > 其他好文 > 详细

反射List<M> To DataTable|反射IList To DataTable|反射 DataTable To List<M>

时间:2014-09-01 10:41:52      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   ar   for   数据   art   

 将DataTable集合反射获取 List<M>

bubuko.com,布布扣
 /// <summary>
        /// 根据DataTable集合反射获取 List<M>
        /// </summary>
        /// <typeparam name="M">泛型实体</typeparam>
        /// <param name="dt">DataTable</param>
        /// <returns>实体集合</returns>
        private static List<M> SetValueRow<M>(DataTable dt) where M : new()
        {
            List<M> list = new List<M>();

            Type type;
            PropertyInfo p;
            M m;

            foreach (DataRow row in dt.Rows)
            {
                m = new M();
                type = m.GetType();

                foreach (DataColumn col in dt.Columns)
                {
                    //获取一个字段的属性
                    p = type.GetProperty(col.ColumnName);

                    //实体中无对应属性
                    if (p == null)
                        continue;

                    string colDbType = row[col.ColumnName].GetType().FullName;

                    //结果集单元格中的值不为空时才赋值
                    if (colDbType != "System.DBNull")
                    {
                        switch (p.PropertyType.FullName)
                        {
                            case "System.Int64"://根据不同数据库数据类型作转换,如oracle的number(2)应转换为Int32,而不是默认的Decemal 
                                p.SetValue(m, Convert.ToInt64(row[col.ColumnName]), null);
                                break;

                            case "System.Int32":
                                p.SetValue(m, Convert.ToInt32(row[col.ColumnName]), null);
                                break;

                            case "System.Int16":
                                p.SetValue(m, Convert.ToInt16(row[col.ColumnName]), null);
                                break;

                            case "System.String":
                                p.SetValue(m, Convert.ToString(row[col.ColumnName]), null);
                                break;

                            case "System.Decimal":
                                p.SetValue(m, Convert.ToDecimal(row[col.ColumnName]), null);
                                break;

                            case "System.DateTime":
                                p.SetValue(m, row[col.ColumnName], null);
                                break;

                            case "System.Double":
                                p.SetValue(m, Convert.ToDouble(row[col.ColumnName]), null);
                                break;

                            case "System.Boolean":
                                p.SetValue(m, Convert.ToBoolean(row[col.ColumnName]), null);
                                break;

                            case "System.Byte":
                                p.SetValue(m, Convert.ToByte(row[col.ColumnName]), null);
                                break;

                            default:
                                p.SetValue(m, row[col.ColumnName], null);
                                break;
                        }
                    }
                }

                list.Add(m);
            }

            return list;
        }
        
View Code

 将IList集合类转换成DataTable  

bubuko.com,布布扣
/// <summary>  
        /// 将IList集合类转换成DataTable  
        /// </summary>  
        /// <param name="list">集合</param>  
        /// <returns></returns>  
        public static DataTable IListToDataTable(IList list)
        {
            DataTable result = new DataTable();
            if (list.Count > 0)
            {
                PropertyInfo[] propertys = list[0].GetType().GetProperties();
                foreach (PropertyInfo pi in propertys)
                {
                    result.Columns.Add(pi.Name, pi.PropertyType);
                }

                for (int i = 0; i < list.Count; i++)
                {
                    ArrayList tempList = new ArrayList();
                    foreach (PropertyInfo pi in propertys)
                    {
                        object obj = pi.GetValue(list[i], null);
                        tempList.Add(obj);
                    }
                    object[] array = tempList.ToArray();
                    result.LoadDataRow(array, true);
                }
            }
            return result;
        }
View Code
将List<M>集合类转换成DataTable 
bubuko.com,布布扣
        /// <summary>  
        /// 将List<M>集合类转换成DataTable  
        /// </summary>  
        /// <param name="list">集合</param>  
        /// <returns></returns>  
        public static DataTable IListToDataTable<M>(List<M> list)
        {
            DataTable result = new DataTable();
            if (list.Count > 0)
            {
                PropertyInfo[] propertys = list[0].GetType().GetProperties();
                foreach (PropertyInfo pi in propertys)
                {
                    result.Columns.Add(pi.Name, pi.PropertyType);
                }

                for (int i = 0; i < list.Count; i++)
                {
                    ArrayList tempList = new ArrayList();
                    foreach (PropertyInfo pi in propertys)
                    {
                        object obj = pi.GetValue(list[i], null);
                        tempList.Add(obj);
                    }
                    object[] array = tempList.ToArray();
                    result.LoadDataRow(array, true);
                }
            }
            return result;
        }
View Code

反射List<M> To DataTable|反射IList To DataTable|反射 DataTable To List<M>

标签:style   blog   http   color   os   ar   for   数据   art   

原文地址:http://www.cnblogs.com/yonsy/p/2654814.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!