首页 > 其他 > 详细

DataReader、Table、DataSet和Entity相互转化

时间:2014-10-03 18:16:05      阅读:320      评论:0      收藏:0      [点我收藏+]
 public class CommonService
    {
        #region DataReader转化
        /// <summary>
        /// 将DataReader转化为Table
        /// </summary>
        /// <param name="reader"></param>
        /// <returns></returns>
        public static DataTable DataReaderToTable(SqlDataReader reader)
        {
            var dt = new DataTable();
            if (reader.HasRows)
            {
                for (int i = 0; i < reader.FieldCount; i++)
                {
                    var column = new DataColumn();
                    column.DataType = reader.GetFieldType(i);
                    column.ColumnName = reader.GetName(i);

                    dt.Columns.Add(column);
                }
                while (reader.Read())
                {
                    object[] rowObjects = new object[reader.FieldCount];
                    for (int i = 0; i < reader.FieldCount; i++)
                    {
                        rowObjects.SetValue(reader.GetValue(i), i);
                    }

                    dt.LoadDataRow(rowObjects, true);
                }
            }
            else
            {
                dt = null;
            }

            return dt;
        }


        /// <summary>
        /// 将DataReader转化为Entity
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="reader"></param>
        /// <returns></returns>
        public static List<T> DataReaderToEntity<T>(SqlDataReader reader) where T : new()
        {
            var list = new List<T>();
            T t = default(T);

            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    t = (T)Activator.CreateInstance(typeof(T));
                    for (int i = 0; i < reader.FieldCount; i++)
                    {
                        PropertyInfo property = t.GetType().GetProperty(reader.GetName(i), BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase);
                        if (Convert.IsDBNull(reader.GetValue(i)))
                        {
                            property.SetValue(t, null, null);
                        }
                        else
                        {
                            property.SetValue(t, reader.GetValue(i), null);
                        }

                        list.Add(t);
                    }
                }
            }

            return list;
        }
        #endregion

        #region DataTable转化
        /// <summary>
        /// Table转化为Entity
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="table"></param>
        /// <returns></returns>
        public static List<T> TableToEntity<T>(DataTable table) where T : new()
        {
            var list = new List<T>();
            T t = default(T);
            t = (T)Activator.CreateInstance(typeof(T));

            if (table == null || table.Rows.Count == 0)
            {

            }
            else
            {
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    foreach (DataColumn column in table.Columns)
                    {
                        PropertyInfo propertyInfo = t.GetType().GetProperty(column.ColumnName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
                        if (Convert.IsDBNull(table.Rows[i][column.ColumnName]))
                        {
                            propertyInfo.SetValue(t, null, null);
                        }
                        else
                        {
                            propertyInfo.SetValue(t, table.Rows[i][column.ColumnName], null);
                        }
                    }

                    list.Add(t);
                }
            }

            return list;
        }


        /// <summary>
        /// Table转化为DataSet
        /// </summary>
        /// <param name="table"></param>
        /// <returns></returns>
        public static DataSet TableToDataSet(DataTable table)
        {
            var dataSet = new DataSet();
            dataSet.Tables.Add(table);

            return dataSet;
        }
        #endregion

        #region Entity转化为Table

        public static DataTable EntityToTable<T>(List<T> list) where T : new()
        {
            var table = new DataTable();

            if (list == null || list.Count == 0)
            {

            }
            else
            {
                T t = list.FirstOrDefault();
                List<PropertyInfo> propertyInfos = t.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase).ToList();
                foreach (var propertyInfo in propertyInfos)
                {
                    table.Columns.Add(propertyInfo.Name, propertyInfo.PropertyType);
                }
                object[] objects = new object[propertyInfos.Count];

                for (int i = 0; i < list.Count; i++)
                {
                    foreach (var propertyInfo in propertyInfos)
                    {
                        objects.SetValue(propertyInfo.GetValue(list[i], null), propertyInfos.IndexOf(propertyInfo));
                    }

                    table.LoadDataRow(objects, true);
                }
            }

            return table;
        }

        #endregion
    }

 

DataReader、Table、DataSet和Entity相互转化

原文:http://www.cnblogs.com/zhouyunbaosujina/p/4005072.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!