• 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
        }
  • 相关阅读:
    JaunsGraph数据模型
    JanusGraph的schema及数据建模
    JanusGraph Server配置
    JanusGraph与Cassandra集成模式
    cassandra的primary key, partition key, cluster key,
    Predix Asset Service深度分析
    Predix中模型设计
    web工程中web.xml元素加载顺序以及配置实例
    Tomcat,Jboss,Glassfish等web容器比较选型
    intelliJ idea像eclipse一样在class中断点调试
  • 原文地址:https://www.cnblogs.com/zhouyunbaosujina/p/4005072.html
Copyright © 2020-2023  润新知