///
/// datatable與實體類互相轉換
///
/// 實體類
public class modelhandlerwhere t : new()
else
}///
/// 填充物件列表:用dataset的第index個表填充實體類
///
public listfillmodel(dataset ds, int index)
else
}///
/// 填充物件列表:用datatable填充實體類
///
public listfillmodel(datatable dt)
listmodellist = new list();
foreach (datarow dr in dt.rows)
modellist.add(model);
}return modellist;
}///
/// 填充物件:用datarow填充實體類
///
public t fillmodel(datarow dr)
//t model = (t)activator.createinstance(typeof(t));
t model = new t();
for (int i = 0; i < dr.table.columns.count; i++)
return model;
}#endregion
#region 實體類轉換成datatable
///
/// 實體類轉換成dataset
///
/// 實體類列表
///
public dataset filldataset(listmodellist)
else
}///
/// 實體類轉換成datatable
///
/// 實體類列表
///
public datatable filldatatable(listmodellist)
datatable dt = createdata(modellist[0]);
foreach(t model in modellist)
dt.rows.add(datarow);
}return dt;
}///
/// 根據實體類得到表結構
///
/// 實體類
///
private datatable createdata(t model)
return datatable;
}#endregion
}///
/// datatable與實體類互相轉換
///
/// 實體類
public class modelhandlerwhere t : new()
else
}///
/// 填充物件列表:用dataset的第index個表填充實體類
///
public listfillmodel(dataset ds, int index)
else
}///
/// 填充物件列表:用datatable填充實體類
///
public listfillmodel(datatable dt)
listmodellist = new list();
foreach (datarow dr in dt.rows)
modellist.add(model);
}return modellist;
}///
/// 填充物件:用datarow填充實體類
///
public t fillmodel(datarow dr)
//t model = (t)activator.createinstance(typeof(t));
t model = new t();
for (int i = 0; i < dr.table.columns.count; i++)
return model;
}#endregion
#region 實體類轉換成datatable
///
/// 實體類轉換成dataset
///
/// 實體類列表
///
public dataset filldataset(listmodellist)
else
}///
/// 實體類轉換成datatable
///
/// 實體類列表
///
public datatable filldatatable(listmodellist)
datatable dt = createdata(modellist[0]);
foreach(t model in modellist)
dt.rows.add(datarow);
}return dt;
}///
/// 根據實體類得到表結構
///
/// 實體類
///
private datatable createdata(t model)
return datatable;
}#endregion
}
json與datatable的相互轉換
region 將json轉換為datatable 將json轉換為datatable 得到的json private datatable jsontodatatable string strjson matchcollection mc rg.matches strjson for int i 0 ...
DataTable與Xml的相互轉化
using system using system.collections.generic using system.linq using system.text using system.data namespace test2 new person datatable dt new datata...
List與DataTable相互轉換
csharp view plain copy print?public static class datatableextensions foreach var item inlist return dt datatable 轉換為list 集合 型別 datatable public static...