• [转载]DataTable与实体类互相转换


    using System.Runtime.Serialization;

        /// <summary>
        /// DataTable与实体类互相转换
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        public class ModelHandler<T> where T : new()
        {
            #region DataTable转换成实体类

            /// <summary>
            /// 填充对象列表:用DataSet的第一个表填充实体类
            /// </summary>
            /// <param name="ds">DataSet</param>
            /// <returns></returns>
            public 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 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 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();
                    foreach (PropertyInfo propertyInfo in typeof(T).GetProperties())
                    {
                        model.GetType().GetProperty(propertyInfo.Name).SetValue(model, dr[propertyInfo.Name], null);
                    }
                    modelList.Add(model);
                }
                return modelList;
            }

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

                //T model = (T)Activator.CreateInstance(typeof(T));  
                T model = new T();
                foreach (PropertyInfo propertyInfo in typeof(T).GetProperties())
                {
                    model.GetType().GetProperty(propertyInfo.Name).SetValue(model, dr[propertyInfo.Name], null);
                }
                return model;
            }

            #endregion

            #region 实体类转换成DataTable

            /// <summary>
            /// 实体类转换成DataSet
            /// </summary>
            /// <param name="modelList">实体类列表</param>
            /// <returns></returns>
            public DataSet FillDataSet(List<T> modelList)
            {
                if (modelList == null || modelList.Count == 0)
                {
                    return null;
                }
                else
                {
                    DataSet ds = new DataSet();
                    ds.Tables.Add(FillDataTable(modelList));
                    return ds;
                }
            }

            /// <summary>
            /// 实体类转换成DataTable
            /// </summary>
            /// <param name="modelList">实体类列表</param>
            /// <returns></returns>
            public DataTable FillDataTable(List<T> modelList)
            {
                if (modelList == null || modelList.Count == 0)
                {
                    return null;
                }
                DataTable dt = CreateData(modelList[0]);

                foreach(T model in modelList)
                {
                    DataRow dataRow = dt.NewRow();
                    foreach (PropertyInfo propertyInfo in typeof(T).GetProperties())
                    {
                        dataRow[propertyInfo.Name] = propertyInfo.GetValue(model, null);
                    }
                    dt.Rows.Add(dataRow);
                }
                return dt;
            }

            /// <summary>
            /// 根据实体类得到表结构
            /// </summary>
            /// <param name="model">实体类</param>
            /// <returns></returns>
            private DataTable CreateData(T model)
            {
                DataTable dataTable = new DataTable(typeof (T).Name);
                foreach (PropertyInfo propertyInfo in typeof(T).GetProperties())
                {
                    dataTable.Columns.Add(new DataColumn(propertyInfo.Name, propertyInfo.PropertyType));
                }
                return dataTable;
            }

            #endregion
        }
     
        如果是三层架构,要实现Model类和Table之间的转换。(目前仅限单行数据表)
     

    /// <summary>
    /// 单行DataTable填充实体类
    /// </summary>
    /// <param name="dt"></param>
    /// <returns></returns>
    public Calendar.Models.Calendar FillModel(DataTable dt)
    {
    if (dt == null || dt.Rows.Count == 0)
    {
    return null;
    }
    Calendar.Models.Calendar modelList = new Calendar.Models.Calendar();
    foreach (DataRow dr in dt.Rows)
    {
    modelList.Id = Convert.ToInt32(dt.Rows[0]["Id"].ToString());
    modelList.Subject = dt.Rows[0]["Subject"].ToString();
    modelList.Location = dt.Rows[0]["Location"].ToString();
    modelList.MasterId = Convert.ToInt32(dt.Rows[0]["MasterId"].ToString());
    modelList.Description = dt.Rows[0]["Description"].ToString();
    modelList.CalendarType = Convert.ToInt32(dt.Rows[0]["CalendarType"].ToString());
    modelList.StartTime = Convert.ToDateTime(dt.Rows[0]["StartTime"].ToString());
    modelList.EndTime = Convert.ToDateTime(dt.Rows[0]["EndTime"].ToString());
    modelList.IsAllDayEvent = Convert.ToBoolean(dt.Rows[0]["IsAllDayEvent"].ToString());
    modelList.HasAttachment = Convert.ToBoolean(dt.Rows[0]["HasAttachment"].ToString());
    modelList.Category = dt.Rows[0]["Category"].ToString();
    modelList.InstanceType = Convert.ToInt32(dt.Rows[0]["InstanceType"].ToString());
    modelList.Attendees = dt.Rows[0]["Attendees"].ToString();
    modelList.AttendeeNames = dt.Rows[0]["AttendeeNames"].ToString();
    modelList.OtherAttendee = dt.Rows[0]["OtherAttendee"].ToString();
    modelList.UPAccount = dt.Rows[0]["UPAccount"].ToString();
    modelList.UPName = dt.Rows[0]["UPName"].ToString();
    modelList.UPTime = Convert.ToDateTime(dt.Rows[0]["UPTime"].ToString());
    modelList.RecurringRule = dt.Rows[0]["RecurringRule"].ToString();
    modelList.State = Convert.ToInt32(dt.Rows[0]["State"].ToString());
    }
    return modelList;
    }

     
     
  • 相关阅读:
    Codeforces 1291 Round #616 (Div. 2) B
    总结
    刷新DNS解析缓存+追踪+域名解析命令
    数学--数论--Hdu 5793 A Boring Question (打表+逆元)
    Lucene.net(4.8.0) 学习问题记录六:Lucene 的索引系统和搜索过程分析
    LeetCode 117 Populating Next Right Pointers in Each Node II
    LeetCode 116 Populating Next Right Pointers in Each Node
    test test
    LeetCode 115 Distinct Subsequences
    LeetCode 114. Flatten Binary Tree to Linked List
  • 原文地址:https://www.cnblogs.com/zqn518/p/2397514.html
Copyright © 2020-2023  润新知