首页 > 其他 > 详细

List转换成DataSet实现代码

时间:2015-01-23 13:15:04      阅读:135      评论:0      收藏:0      [点我收藏+]

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; 

List转换成DataSet实现代码

原文:http://www.cnblogs.com/lxboy2009/p/4243698.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!