//dataset與泛型集合間的互相轉換
//利用反射機制將datatable的字段與自定義型別的公開屬性互相賦值。
//注意:從dataset到ilist的轉換,自定義型別的公開屬性必須與datatable中的欄位名稱
//**說明
///
/// 泛型集合與dataset互相轉換
///
using system.data;
using system.reflection;
using system.collections;
using system.collections.generic;
using system;
namespace qdl
{public class ilistdataset
{///
/// 集合裝換dataset
///
/// 集合
///
/// 2008-8-1 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;
///
/// 泛型集合轉換dataset
///
///
/// 泛型集合
///
/// 2008-8-1 22:43 hpdv2806
public static dataset todataset(ilistlist)
{return todataset(list, null);
///
/// 泛型集合轉換dataset
///
///
/// 泛型集合
/// 待轉換屬性名陣列
///
/// 2008-8-1 22:44 hpdv2806
public static dataset todataset(ilistp_list, params string p_propertyname)
{listpropertynamelist = new list();
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;
///
/// dataset裝換為泛型集合
///
///
/// dataset
/// 待轉換資料表索引
///
/// 2008-8-1 22:46 hpdv2806
public static ilistdatasettoilist(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];
// 返回值初始化
ilistresult = new list();
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;
///
/// dataset裝換為泛型集合
///
///
/// dataset
/// 待轉換資料表名稱
///
/// 2008-8-1 22:47 hpdv2806
public static ilistdatasettoilist(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(p_dataset, _tableindex);
使用範圍
1 可以用在業務層中資料獲取,獲取dataset的同時也可以轉為ilist集合為呼叫者所使用。
2 在webservices中傳輸自定義型別使用,即傳遞引數都用dataset型別(webservices直接支援的資料型別),在使用前將其轉換為ilist來使用
DataSet與泛型集合間的轉換
public class ilistdataset for int i 0 i p list.count i object array templist.toarray datatable.loaddatarow array,true result.tables.add datatable retu...
DataSet與泛型集合間的轉換
public class ilistdataset for int i 0 i p list.count i object array templist.toarray datatable.loaddatarow array,true result.tables.add datatable retu...
DataSet與泛型集合間的轉換
public class ilistdataset for int i 0 i p list.count i object array templist.toarray datatable.loaddatarow array,true result.tables.add datatable retu...