DataReader、Table、DataSet和Entity相互转化

 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
    }
原文地址:https://www.cnblogs.com/zhouyunbaosujina/p/4005072.html