DataSet與泛型集合間的互相轉換

哈哈哈哈哈我撒發表於2009-09-01

在網上曾經看到一個將IList型別轉換為DataSet的文章,自己又再其基礎之上擴充套件了一下,將DataSet轉換為IList泛型集合的過程也實現了。 

基本思路 

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

程式碼說明 



/// <summary> 
/// 泛型集合與DataSet互相轉換 
/// </summary> 
public class IListDataSet 


/// <summary> 
/// 集合裝換DataSet 
/// </summary> 
/// <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(); 
foreach ( PropertyInfo pi in propertys ) 

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; 


/// <summary> 
/// 泛型集合轉換DataSet 
/// </summary> 
/// <typeparam name="T"></typeparam> 
/// <param name="list">泛型集合</param> 
/// <returns></returns> 
/// 2008-08-01 22:43 HPDV2806 
public static DataSet ToDataSet<T>( IList<T> list ) 

return ToDataSet<T>( list, null ); 



/// <summary> 
/// 泛型集合轉換DataSet 
/// </summary> 
/// <typeparam name="T"></typeparam> 
/// <param name="p_List">泛型集合</param> 
/// <param name="p_PropertyName">待轉換屬性名陣列</param> 
/// <returns></returns> 
/// 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 ); 

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 ) 

if ( propertyNameList.Count == 0 ) 

// 沒有指定屬性的情況下全部屬性都要轉換 
_DataTable.Columns.Add( pi.Name, pi.PropertyType ); 

else 

if ( propertyNameList.Contains( pi.Name ) ) 
_DataTable.Columns.Add( pi.Name, pi.PropertyType ); 



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

ArrayList tempList = new ArrayList(); 
foreach ( PropertyInfo pi in propertys ) 

if ( propertyNameList.Count == 0 ) 

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

else 

if ( propertyNameList.Contains( pi.Name ) ) 

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; 


/// <summary> 
/// DataSet裝換為泛型集合 
/// </summary> 
/// <typeparam name="T"></typeparam> 
/// <param name="p_DataSet">DataSet</param> 
/// <param name="p_TableIndex">待轉換資料表索引</param> 
/// <returns></returns> 
/// 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 ) 
return null; 
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(); 
foreach ( PropertyInfo pi in propertys ) 

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 ); 
else 
pi.SetValue( _t, null, null ); 
break; 



result.Add( _t ); 

return result; 


/// <summary> 
/// DataSet裝換為泛型集合 
/// </summary> 
/// <typeparam name="T"></typeparam> 
/// <param name="p_DataSet">DataSet</param> 
/// <param name="p_TableName">待轉換資料表名稱</param> 
/// <returns></returns> 
/// 2008-08-01 22:47 HPDV2806 
public static IList<T> DataSetToIList<T>( DataSet p_DataSet, string p_TableName ) 

int _TableIndex = 0; 
if ( p_DataSet == null || p_DataSet.Tables.Count < 0 ) 
return null; 
if ( string.IsNullOrEmpty( p_TableName ) ) 
return null; 
for ( int i = 0; i < p_DataSet.Tables.Count; i++ ) 

// 獲取Table名稱在Tables集合中的索引值 
if ( p_DataSet.Tables[i].TableName.Equals( p_TableName ) ) 

_TableIndex = i; 
break; 


return DataSetToIList<T>( p_DataSet, _TableIndex ); 





使用範圍 

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

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

相關文章