DataSet与泛型集合间的互相转换

作者:veryhappy(wx.net)

 

转载请注明!

很久没有写技术文章了,刚刚研究点小东西拿出来和大家分享。

概述

在网上曾经看到一个将IList类型转换为DataSet的文章,自己又再其基础之上扩展了一下,将DataSet转换为IList泛型集合的过程也实现了。

基本思路

利用反射机制将DataTable的字段与自定义类型的公开属性互相赋值。注意:从DataSetIList<T>的转换,自定义类型的公开属性必须与DataTable中的字段名称一致,才能到达想要的结果。建议DataTable的定义从数据库来,自定义类型用O/R Mapping的方式获得

代码说明

 

  1. /// <summary>
  2.     /// 泛型集合与DataSet互相转换
  3.     /// </summary>
  4.     public class IListDataSet
  5.     {
  6.         /// <summary>
  7.         /// 集合装换DataSet
  8.         /// </summary>
  9.         /// <param name="list">集合</param>
  10.         /// <returns></returns>
  11.         /// 2008-08-01 22:08 HPDV2806
  12.         public static DataSet ToDataSet( IList p_List )
  13.         {
  14.             DataSet result = new DataSet();
  15.             DataTable _DataTable = new DataTable();
  16.             if ( p_List.Count > 0 )
  17.             {
  18.                 PropertyInfo[] propertys = p_List[0].GetType().GetProperties();
  19.                 foreach ( PropertyInfo pi in propertys )
  20.                 {
  21.                     _DataTable.Columns.Add( pi.Name, pi.PropertyType );
  22.                 }
  23.                 for ( int i = 0; i < p_List.Count; i++ )
  24.                 {
  25.                     ArrayList tempList = new ArrayList();
  26.                     foreach ( PropertyInfo pi in propertys )
  27.                     {
  28.                         object obj = pi.GetValue( p_List[i], null );
  29.                         tempList.Add( obj );
  30.                     }
  31.                     object[] array = tempList.ToArray();
  32.                     _DataTable.LoadDataRow( array, true );
  33.                 }
  34.             }
  35.             result.Tables.Add( _DataTable );
  36.             return result;
  37.         }
  38.         /// <summary>
  39.         /// 泛型集合转换DataSet
  40.         /// </summary>
  41.         /// <typeparam name="T"></typeparam>
  42.         /// <param name="list">泛型集合</param>
  43.         /// <returns></returns>
  44.         /// 2008-08-01 22:43 HPDV2806
  45.         public static DataSet ToDataSet<T>( IList<T> list )
  46.         {
  47.             return ToDataSet<T>( list, null );
  48.         }
  49.         /// <summary>
  50.         /// 泛型集合转换DataSet
  51.         /// </summary>
  52.         /// <typeparam name="T"></typeparam>
  53.         /// <param name="p_List">泛型集合</param>
  54.         /// <param name="p_PropertyName">待转换属性名数组</param>
  55.         /// <returns></returns>
  56.         /// 2008-08-01 22:44 HPDV2806
  57.         public static DataSet ToDataSet<T>( IList<T> p_List, params string[] p_PropertyName )
  58.         {
  59.             List<string> propertyNameList = new List<string>();
  60.             if ( p_PropertyName != null )
  61.                 propertyNameList.AddRange( p_PropertyName );
  62.             DataSet result = new DataSet();
  63.             DataTable _DataTable = new DataTable();
  64.             if ( p_List.Count > 0 )
  65.             {
  66.                 PropertyInfo[] propertys = p_List[0].GetType().GetProperties();
  67.                 foreach ( PropertyInfo pi in propertys )
  68.                 {
  69.                     if ( propertyNameList.Count == 0 )
  70.                     {
  71.                         // 没有指定属性的情况下全部属性都要转换
  72.                         _DataTable.Columns.Add( pi.Name, pi.PropertyType );
  73.                     }
  74.                     else
  75.                     {
  76.                         if ( propertyNameList.Contains( pi.Name ) )
  77.                             _DataTable.Columns.Add( pi.Name, pi.PropertyType );
  78.                     }
  79.                 }
  80.                 for ( int i = 0; i < p_List.Count; i++ )
  81.                 {
  82.                     ArrayList tempList = new ArrayList();
  83.                     foreach ( PropertyInfo pi in propertys )
  84.                     {
  85.                         if ( propertyNameList.Count == 0 )
  86.                         {
  87.                             object obj = pi.GetValue( p_List[i], null );
  88.                             tempList.Add( obj );
  89.                         }
  90.                         else
  91.                         {
  92.                             if ( propertyNameList.Contains( pi.Name ) )
  93.                             {
  94.                                 object obj = pi.GetValue( p_List[i], null );
  95.                                 tempList.Add( obj );
  96.                             }
  97.                         }
  98.                     }
  99.                     object[] array = tempList.ToArray();
  100.                     _DataTable.LoadDataRow( array, true );
  101.                 }
  102.             }
  103.             result.Tables.Add( _DataTable );
  104.             return result;
  105.         }
  106.         /// <summary>
  107.         /// DataSet装换为泛型集合
  108.         /// </summary>
  109.         /// <typeparam name="T"></typeparam>
  110.         /// <param name="p_DataSet">DataSet</param>
  111.         /// <param name="p_TableIndex">待转换数据表索引</param>
  112.         /// <returns></returns>
  113.         /// 2008-08-01 22:46 HPDV2806
  114.         public static IList<T> DataSetToIList<T>( DataSet p_DataSet, int p_TableIndex )
  115.         {
  116.             if ( p_DataSet == null || p_DataSet.Tables.Count < 0 )
  117.                 return null;
  118.             if ( p_TableIndex > p_DataSet.Tables.Count - 1 )
  119.                 return null;
  120.             if ( p_TableIndex < 0 )
  121.                 p_TableIndex = 0;
  122.             DataTable p_Data = p_DataSet.Tables[p_TableIndex];
  123.             // 返回值初始化
  124.             IList<T> result = new List<T>();
  125.             for ( int j = 0; j < p_Data.Rows.Count; j++ )
  126.             {
  127.                 T _t = (T)Activator.CreateInstance( typeof( T ) );
  128.                 PropertyInfo[] propertys = _t.GetType().GetProperties();
  129.                 foreach ( PropertyInfo pi in propertys )
  130.                 {
  131.                     for ( int i = 0; i < p_Data.Columns.Count; i++ )
  132.                     {
  133.                         // 属性与字段名称一致的进行赋值
  134.                         if ( pi.Name.Equals( p_Data.Columns[i].ColumnName ) )
  135.                         {
  136.                             // 数据库NULL值单独处理
  137.                             if ( p_Data.Rows[j][i] != DBNull.Value )
  138.                                 pi.SetValue( _t, p_Data.Rows[j][i], null );
  139.                             else
  140.                                 pi.SetValue( _t, nullnull );
  141.                             break;
  142.                         }
  143.                     }
  144.                 }
  145.                 result.Add( _t );
  146.             }
  147.             return result;
  148.         }
  149.         /// <summary>
  150.         /// DataSet装换为泛型集合
  151.         /// </summary>
  152.         /// <typeparam name="T"></typeparam>
  153.         /// <param name="p_DataSet">DataSet</param>
  154.         /// <param name="p_TableName">待转换数据表名称</param>
  155.         /// <returns></returns>
  156.         /// 2008-08-01 22:47 HPDV2806
  157.         public static IList<T> DataSetToIList<T>( DataSet p_DataSet, string p_TableName )
  158.         {
  159.             int _TableIndex = 0;
  160.             if ( p_DataSet == null || p_DataSet.Tables.Count < 0 )
  161.                 return null;
  162.             if ( string.IsNullOrEmpty( p_TableName ) )
  163.                 return null;
  164.             for ( int i = 0; i < p_DataSet.Tables.Count; i++ )
  165.             {
  166.                 // 获取Table名称在Tables集合中的索引值
  167.                 if ( p_DataSet.Tables[i].TableName.Equals( p_TableName ) )
  168.                 {
  169.                     _TableIndex = i;
  170.                     break;
  171.                 }
  172.             }
  173.             return DataSetToIList<T>( p_DataSet, _TableIndex );
  174.         }
  175. }

 

使用范围

1.       可以用在业务层中数据获取,获取DataSet的同时也可以转为IList集合为调用者所使用。

2.       WebServices中传输自定义类型使用,即传递参数都用DataSet类型(WebServices直接支持的数据类型),在使用前将其转换为IList来使用。

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值