http://xiangai.taobao.com
http://shop148612228.taobao.com

Ilist<T> 转换成 DataSet

/// <summary>
/// Ilist<T> 转换成 DataSet
/// </summary>
/// <param name="list"></param>
/// <returns></returns>

public static 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;
}

posted @ 2011-10-23 23:09  万事俱备就差个程序员  阅读(234)  评论(0编辑  收藏  举报

http://xiangai.taobao.com
http://shop148612228.taobao.com
如果您觉得对您有帮助.领个红包吧.谢谢.
支付宝红包
微信打赏 支付宝打赏