//DataSet與泛型集合間的互相轉換
//利用反射機制將DataTable的字段與自定義類型的公開屬性互相賦值。
//注意:從DataSet到IList<T>的轉換,自定義類型的公開屬性必須與DataTable中的字段名稱
//一致,才能到達想要的結果。建議DataTable的定義從數據庫來,自定義類型用O/R Mapping的方式獲得。
//代碼說明
/// <summary>
/// 泛型集合與DataSet互相轉換
/// </summary>
using System.Data;
using System.Reflection;
using System.Collections;
using System.Collections.Generic;
using System;
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 泛型之間互相轉換
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.