天天看點

DataSet與泛型集合間的轉換

基本思路

利用反射機制将DataTable的字段與自定義類型的公開屬性互相指派。注意:從DataSet到IList<T>的轉換,自定義類型的公開屬性必須與DataTable中的字段名稱一緻,才能到達想要的結果。建議DataTable的定義從資料庫來,自定義類型用O/R Mapping的方式獲得。

代碼說明

/// <summary>

/// 泛型集合與DataSet互相轉換

/// </summary>

public class IListDataSet

{

/// 集合裝換DataSet

/// <param name="list">集合</param>

/// <returns></returns>

/// 2008-08-01 22:08 HPDV2806

public static DataSet ToDataSet( IList p_List )

DataSet result = new DataSet();

DataTable _DataTable = new DataTable();

if ( p_List.Count > 0 )

PropertyInfo[] propertys = p_List[0].GetType().GetProperties();

foreach ( PropertyInfo pi in propertys )

_DataTable.Columns.Add( pi.Name, pi.PropertyType );

}

for ( int i = 0; i < p_List.Count; i++ )

ArrayList tempList = new ArrayList();

object obj = pi.GetValue( p_List[i], null );

tempList.Add( obj );

object[] array = tempList.ToArray();

_DataTable.LoadDataRow( array, true );

result.Tables.Add( _DataTable );

return result;

/// 泛型集合轉換DataSet

/// <typeparam name="T"></typeparam>

/// <param name="list">泛型集合</param>

/// 2008-08-01 22:43 HPDV2806

public static DataSet ToDataSet<T>( IList<T> list )

return ToDataSet<T>( list, null );

/// <param name="p_List">泛型集合</param>

/// <param name="p_PropertyName">待轉換屬性名數組</param>

/// 2008-08-01 22:44 HPDV2806

public static DataSet ToDataSet<T>( IList<T> p_List, params string[] p_PropertyName )

List<string> propertyNameList = new List<string>();

if ( p_PropertyName != null )

propertyNameList.AddRange( p_PropertyName );

if ( propertyNameList.Count == 0 )

// 沒有指定屬性的情況下全部屬性都要轉換

else

if ( propertyNameList.Contains( pi.Name ) )

/// DataSet裝換為泛型集合

/// <param name="p_DataSet">DataSet</param>

/// <param name="p_TableIndex">待轉換資料表索引</param>

/// 2008-08-01 22:46 HPDV2806

public static IList<T> DataSetToIList<T>( DataSet p_DataSet, int p_TableIndex )

if ( p_DataSet == null || p_DataSet.Tables.Count < 0 )

return null;

if ( p_TableIndex > p_DataSet.Tables.Count - 1 )

if ( p_TableIndex < 0 )

p_TableIndex = 0;

DataTable p_Data = p_DataSet.Tables[p_TableIndex];

// 傳回值初始化

IList<T> result = new List<T>();

for ( int j = 0; j < p_Data.Rows.Count; j++ )

T _t = (T)Activator.CreateInstance( typeof( T ) );

PropertyInfo[] propertys = _t.GetType().GetProperties();

for ( int i = 0; i < p_Data.Columns.Count; i++ )

// 屬性與字段名稱一緻的進行指派

if ( pi.Name.Equals( p_Data.Columns[i].ColumnName ) )

// 資料庫NULL值單獨處理

if ( p_Data.Rows[j][i] != DBNull.Value )

pi.SetValue( _t, p_Data.Rows[j][i], null );

pi.SetValue( _t, null, null );

break;

result.Add( _t );

/// <param name="p_TableName">待轉換資料表名稱</param>

/// 2008-08-01 22:47 HPDV2806

public static IList<T> DataSetToIList<T>( DataSet p_DataSet, string p_TableName )

int _TableIndex = 0;

if ( string.IsNullOrEmpty( p_TableName ) )

for ( int i = 0; i < p_DataSet.Tables.Count; i++ )

// 擷取Table名稱在Tables集合中的索引值

if ( p_DataSet.Tables[i].TableName.Equals( p_TableName ) )

_TableIndex = i;

return DataSetToIList<T>( p_DataSet, _TableIndex );

使用範圍

1. 可以用在業務層中資料擷取,擷取DataSet的同時也可以轉為IList集合為調用者所使用。

2. 在WebServices中傳輸自定義類型使用,即傳遞參數都用DataSet類型(WebServices直接支援的資料類型),在使用前将其轉換為IList來使用。

版權

作者:靈動生活 郝憲玮

如果你認為此文章有用,請點選底端的【推薦】讓其他人也了解此文章,

DataSet與泛型集合間的轉換

本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接配接,否則保留追究法律責任的權利。