zoukankan      html  css  js  c++  java
  • 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
        }
  • 相关阅读:
    关于图像分类问题读后感
    IO 输入流操作
    BP(back propagation)反向传播
    初识C++的类
    【转】贾扬清:希望Caffe成为深度学习领域的Hadoop
    转:谷歌大脑科学家 Caffe缔造者 贾扬清 微信讲座完整版
    cmd命令行给main传参数
    把vector中的string对象导入到字符指针数组中
    转:字符数组与字符指针
    MHI ,运动历史图像的的获取[下载自CSDN]
  • 原文地址:https://www.cnblogs.com/zhouyunbaosujina/p/4005072.html
Copyright © 2011-2022 走看看