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

DataReader、Table、DataSet和Entity相互转化

时间:2014-10-03 18:25:25      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   os   ar   for   sp   div   

 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相互转化

标签:style   blog   color   io   os   ar   for   sp   div   

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

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