DataSet和List 的相互轉換


/// <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 ); 
} 
} 
DataSet和List的相互轉換

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM