zoukankan      html  css  js  c++  java
  • DataSet与集合的相互转换

      
      1  /// <summary>
      2             /// DataSetToList
      3             /// </summary>
      4             /// <typeparam name="T">转换类型</typeparam>
      5             /// <param name="dataSet">数据源</param>
      6             /// <param name="tableIndex">需要转换表的索引</param>
      7             /// <returns></returns>
      8             public IList<T> DataSetToList<T>(DataSet dataSet, int tableIndex)
      9             {
     10                 //确认参数有效
     11                 if (dataSet == null || dataSet.Tables.Count <= 0 || tableIndex < 0)
     12                     return null;
     13                 DataTable dt = dataSet.Tables[tableIndex];
     14                 IList<T> list = new List<T>();
     15                 for (int i = 0; i < dt.Rows.Count; i++)
     16                 {
     17                     //创建泛型对象
     18                     T _t = Activator.CreateInstance<T>();
     19                     //获取对象所有属性
     20                     PropertyInfo[] propertyInfo = _t.GetType().GetProperties();
     21                     for (int j = 0; j < dt.Columns.Count; j++)
     22                     {
     23                         foreach (PropertyInfo info in propertyInfo)
     24                         {
     25                             //属性名称和列名相同时赋值
     26                             if (dt.Columns[j].ColumnName.ToUpper().Equals(info.Name.ToUpper()))
     27                             {
     28                                 if (dt.Rows[i][j] != DBNull.Value)
     29                                 {
     30                                     info.SetValue(_t, dt.Rows[i][j], null);
     31                                 }
     32                                 else
     33                                 {
     34                                     info.SetValue(_t, null, null);
     35                                 }
     36                                 break;
     37                             }
     38                         }
     39                     }
     40                     list.Add(_t);
     41                 }
     42                 return list;
     43             }
     44 
     45 
     46 /// <summary> 
     47 /// 泛型集合与DataSet互相转换 
     48 /// </summary> 
     49 public class IListDataSet 
     50 { 
     51 
     52 /// <summary> 
     53 /// 集合装换DataSet 
     54 /// </summary> 
     55 /// <param name="list">集合</param> 
     56 /// <returns></returns> 
     57 /// 2008-08-01 22:08 HPDV2806 
     58 public static DataSet ToDataSet( IList p_List ) 
     59 { 
     60 DataSet result = new DataSet(); 
     61 DataTable _DataTable = new DataTable(); 
     62 if ( p_List.Count > 0 ) 
     63 { 
     64 PropertyInfo[] propertys = p_List[0].GetType().GetProperties(); 
     65 foreach ( PropertyInfo pi in propertys ) 
     66 { 
     67 _DataTable.Columns.Add( pi.Name, pi.PropertyType ); 
     68 } 
     69 
     70 for ( int i = 0; i < p_List.Count; i++ ) 
     71 { 
     72 ArrayList tempList = new ArrayList(); 
     73 foreach ( PropertyInfo pi in propertys ) 
     74 { 
     75 object obj = pi.GetValue( p_List[i], null ); 
     76 tempList.Add( obj ); 
     77 } 
     78 object[] array = tempList.ToArray(); 
     79 _DataTable.LoadDataRow( array, true ); 
     80 } 
     81 } 
     82 result.Tables.Add( _DataTable ); 
     83 return result; 
     84 } 
     85 
     86 /// <summary> 
     87 /// 泛型集合转换DataSet 
     88 /// </summary> 
     89 /// <typeparam name="T"></typeparam> 
     90 /// <param name="list">泛型集合</param> 
     91 /// <returns></returns> 
     92 /// 2008-08-01 22:43 HPDV2806 
     93 public static DataSet ToDataSet<T>( IList<T> list ) 
     94 { 
     95 return ToDataSet<T>( list, null ); 
     96 } 
     97 
     98 
     99 /// <summary> 
    100 /// 泛型集合转换DataSet 
    101 /// </summary> 
    102 /// <typeparam name="T"></typeparam> 
    103 /// <param name="p_List">泛型集合</param> 
    104 /// <param name="p_PropertyName">待转换属性名数组</param> 
    105 /// <returns></returns> 
    106 /// 2008-08-01 22:44 HPDV2806 
    107 public static DataSet ToDataSet<T>( IList<T> p_List, params string[] p_PropertyName ) 
    108 { 
    109 List<string> propertyNameList = new List<string>(); 
    110 if ( p_PropertyName != null ) 
    111 propertyNameList.AddRange( p_PropertyName ); 
    112 
    113 DataSet result = new DataSet(); 
    114 DataTable _DataTable = new DataTable(); 
    115 if ( p_List.Count > 0 ) 
    116 { 
    117 PropertyInfo[] propertys = p_List[0].GetType().GetProperties(); 
    118 foreach ( PropertyInfo pi in propertys ) 
    119 { 
    120 if ( propertyNameList.Count == 0 ) 
    121 { 
    122 // 没有指定属性的情况下全部属性都要转换 
    123 _DataTable.Columns.Add( pi.Name, pi.PropertyType ); 
    124 } 
    125 else 
    126 { 
    127 if ( propertyNameList.Contains( pi.Name ) ) 
    128 _DataTable.Columns.Add( pi.Name, pi.PropertyType ); 
    129 } 
    130 } 
    131 
    132 for ( int i = 0; i < p_List.Count; i++ ) 
    133 { 
    134 ArrayList tempList = new ArrayList(); 
    135 foreach ( PropertyInfo pi in propertys ) 
    136 { 
    137 if ( propertyNameList.Count == 0 ) 
    138 { 
    139 object obj = pi.GetValue( p_List[i], null ); 
    140 tempList.Add( obj ); 
    141 } 
    142 else 
    143 { 
    144 if ( propertyNameList.Contains( pi.Name ) ) 
    145 { 
    146 object obj = pi.GetValue( p_List[i], null ); 
    147 tempList.Add( obj ); 
    148 } 
    149 } 
    150 } 
    151 object[] array = tempList.ToArray(); 
    152 _DataTable.LoadDataRow( array, true ); 
    153 } 
    154 } 
    155 result.Tables.Add( _DataTable ); 
    156 return result; 
    157 } 
    158 
    159 /// <summary> 
    160 /// DataSet装换为泛型集合 
    161 /// </summary> 
    162 /// <typeparam name="T"></typeparam> 
    163 /// <param name="p_DataSet">DataSet</param> 
    164 /// <param name="p_TableIndex">待转换数据表索引</param> 
    165 /// <returns></returns> 
    166 /// 2008-08-01 22:46 HPDV2806 
    167 public static IList<T> DataSetToIList<T>( DataSet p_DataSet, int p_TableIndex ) 
    168 { 
    169 if ( p_DataSet == null || p_DataSet.Tables.Count < 0 ) 
    170 return null; 
    171 if ( p_TableIndex > p_DataSet.Tables.Count - 1 ) 
    172 return null; 
    173 if ( p_TableIndex < 0 ) 
    174 p_TableIndex = 0; 
    175 
    176 DataTable p_Data = p_DataSet.Tables[p_TableIndex]; 
    177 // 返回值初始化 
    178 IList<T> result = new List<T>(); 
    179 for ( int j = 0; j < p_Data.Rows.Count; j++ ) 
    180 { 
    181 T _t = (T)Activator.CreateInstance( typeof( T ) ); 
    182 PropertyInfo[] propertys = _t.GetType().GetProperties(); 
    183 foreach ( PropertyInfo pi in propertys ) 
    184 { 
    185 for ( int i = 0; i < p_Data.Columns.Count; i++ ) 
    186 { 
    187 // 属性与字段名称一致的进行赋值 
    188 if ( pi.Name.Equals( p_Data.Columns[i].ColumnName ) ) 
    189 { 
    190 // 数据库NULL值单独处理 
    191 if ( p_Data.Rows[j][i] != DBNull.Value ) 
    192 pi.SetValue( _t, p_Data.Rows[j][i], null ); 
    193 else 
    194 pi.SetValue( _t, null, null ); 
    195 break; 
    196 } 
    197 } 
    198 } 
    199 result.Add( _t ); 
    200 } 
    201 return result; 
    202 } 
    203 
    204 /// <summary> 
    205 /// DataSet装换为泛型集合 
    206 /// </summary> 
    207 /// <typeparam name="T"></typeparam> 
    208 /// <param name="p_DataSet">DataSet</param> 
    209 /// <param name="p_TableName">待转换数据表名称</param> 
    210 /// <returns></returns> 
    211 /// 2008-08-01 22:47 HPDV2806 
    212 public static IList<T> DataSetToIList<T>( DataSet p_DataSet, string p_TableName ) 
    213 { 
    214 int _TableIndex = 0; 
    215 if ( p_DataSet == null || p_DataSet.Tables.Count < 0 ) 
    216 return null; 
    217 if ( string.IsNullOrEmpty( p_TableName ) ) 
    218 return null; 
    219 for ( int i = 0; i < p_DataSet.Tables.Count; i++ ) 
    220 { 
    221 // 获取Table名称在Tables集合中的索引值 
    222 if ( p_DataSet.Tables[i].TableName.Equals( p_TableName ) ) 
    223 { 
    224 _TableIndex = i; 
    225 break; 
    226 } 
    227 } 
    228 return DataSetToIList<T>( p_DataSet, _TableIndex ); 
    229 } 
    230 }
  • 相关阅读:
    WebForm跨页面传值---内置对象
    后台获得集合,变成json字符串,放到EL表达式,js进行获取遍历
    Delphi 编写DLL动态链接库文件的知识和样例(有详细步骤,很清楚)
    DELPHI美化界面(2009开始TPanel增加了ParentBackGround)
    在Delphi中创建线程,请一定使用BeginThread()代替CreateThread()创建线程!(更好的管理异常)
    delphi中最小化其他程序及所有程序最小化(使用 shell.minimizeAll 和自己寻找窗口这两种办法)
    电脑睡眠状态(ACPI规范定义了七个状态,使用powercfg -a进行查看)
    javascript
    Master-Worker模式
    程序员的基础和解决问题的思维
  • 原文地址:https://www.cnblogs.com/caosenianhuan/p/3110217.html
Copyright © 2011-2022 走看看