List转换成DataSet实现代码
时间:2023-03-02 04:46:01
/// <summary>
/// List转换成DataSet
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="list">将要转换的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;
}
标签:List,DataSet
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解Struts2动态方法调用
2022-10-18 11:19:25
Java Http的基础概念了解
2021-08-12 04:23:28
![](https://img.aspxhome.com/file/2023/9/122029_0s.png)
C#中深拷贝和浅拷贝的介绍与用法
2022-11-04 07:42:46
![](https://img.aspxhome.com/file/2023/8/98198_0s.jpg)
2022 最新 IntelliJ IDEA 详细配置步骤演示(推荐)
2021-11-20 21:05:48
![](https://img.aspxhome.com/file/2023/3/77483_0s.jpg)
C#游戏开发之实现贪吃蛇游戏
2023-01-28 01:48:48
![](https://img.aspxhome.com/file/2023/9/83309_0s.png)
Apache Shrio安全框架实现原理及实例详解
2023-07-27 19:47:59
![](https://img.aspxhome.com/file/2023/5/75515_0s.png)
MyBatis执行Sql的流程实例解析
2022-06-12 19:43:42
Java去掉数字字符串开头的0三种方法(推荐)
2022-05-31 08:04:41
Java高并发BlockingQueue重要的实现类详解
2021-06-20 12:08:24
Android使用Websocket实现聊天室
2023-07-07 13:01:59
![](https://img.aspxhome.com/file/2023/6/60386_0s.png)
在maven中引入本地jar包的步骤
2023-11-25 10:43:44
Android使用WindowManager构造悬浮view
2022-08-03 00:43:13
![](https://img.aspxhome.com/file/2023/9/138459_0s.jpg)
C#中GDI+绘制圆弧及圆角矩形等比缩放的绘制
2022-06-14 21:15:05
![](https://img.aspxhome.com/file/2023/3/89823_0s.png)
Android模拟开关按钮点击打开动画(属性动画之平移动画)
2021-09-05 17:48:52
![](https://img.aspxhome.com/file/2023/6/138886_0s.png)
Android 嵌套Fragment的使用实例代码
2022-07-18 06:37:15
![](https://img.aspxhome.com/file/2023/6/111686_0s.png)
SWT(JFace)体验之ViewForm的使用
2023-10-20 13:02:01
![](https://img.aspxhome.com/file/2023/6/89566_0s.jpg)
C# StringBuilder和string
2021-08-20 00:21:20
Spring Boot日志技术logback原理及配置解析
2022-09-14 10:51:57
![](https://img.aspxhome.com/file/2023/2/63422_0s.png)
C# winform实现右下角弹出窗口结果的方法
2023-02-05 14:22:57
C#中文件名或文件路径非法字符判断方法
2023-12-01 14:14:21