List轉(zhuǎn)換成DataSet實(shí)現(xiàn)代碼
更新時(shí)間:2012年12月13日 09:58:19 作者:
怎樣把List轉(zhuǎn)換成DataSet本人很是疑惑,于是搜集整理一番,需要的朋友可以參考下
復(fù)制代碼 代碼如下:
/// <summary>
/// List轉(zhuǎn)換成DataSet
/// </summary>
/// <typeparam name="T">類型</typeparam>
/// <param name="list">將要轉(zhuǎn)換的List</param>
/// <returns></returns>
public DataSet ConvertToDataSet<T>(IList<T> list)
{
if (list == null || list.Count <= 0)
{
return null;
}
DataSet ds = new DataSet();
DataTable dt = new DataTable(typeof(T).Name);
DataColumn column;
DataRow row;
System.Reflection.PropertyInfo[] myPropertyInfo = typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
foreach (T t in list)
{
if (t == null)
{
continue;
}
row = dt.NewRow();
for (int i = 0, j = myPropertyInfo.Length; i < j; i++)
{
System.Reflection.PropertyInfo pi = myPropertyInfo[i];
string name = pi.Name;
if (dt.Columns[name] == null)
{
column = new DataColumn(name, pi.PropertyType);
dt.Columns.Add(column);
}
row[name] = pi.GetValue(t, null);
}
dt.Rows.Add(row);
}
ds.Tables.Add(dt);
return ds;
}
相關(guān)文章
WPF中的ValidationRule實(shí)現(xiàn)參數(shù)綁定解決方案
在WPF中,默認(rèn)情況下,DataContext是通過可視化樹來傳遞的,父元素的DataContext會自動(dòng)傳遞給其子元素,以便子元素可以訪問父元素的數(shù)據(jù)對象,這篇文章主要介紹了WPF中的ValidationRule實(shí)現(xiàn)參數(shù)綁定解決方案,需要的朋友可以參考下2023-08-08關(guān)于C#中GUI編程的標(biāo)準(zhǔn)事件問題
這篇文章主要介紹了C#中GUI編程的標(biāo)準(zhǔn)事件,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-01-01區(qū)分WCF與WebService的異同、優(yōu)勢
這篇文章主要幫助大家區(qū)分WCF與WebService的異同、優(yōu)勢,分為三大方面進(jìn)行研究學(xué)習(xí),感興趣的小伙伴們可以參考一下2016-03-03C#遍歷得到checkboxlist選中值和設(shè)置選中項(xiàng)的代碼
這篇文章主要介紹了C#遍歷得到checkboxlist選中值和設(shè)置選中項(xiàng)的代碼,代碼簡單易懂,具有參考借鑒價(jià)值,需要的朋友可以參考下2016-08-08C#使用動(dòng)態(tài)規(guī)劃解決0-1背包問題實(shí)例分析
這篇文章主要介紹了C#使用動(dòng)態(tài)規(guī)劃解決0-1背包問題,實(shí)例分析了C#動(dòng)態(tài)規(guī)劃算法的實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04