List<T> 转换 DataTable
public class List2DataTable {
public static string GetClassName(Type type)
{ if (type == null)
throw new ArgumentException("参数type不能为空");
if (type.HasAttribute<AliasAttribute>())
return type.GetAttribute<AliasAttribute>().Name.ToLower();
else
return type.Name.ToLower();
}
public static DataTable ListToDataTable<T>(List<T> oList) where T : class
{
try
{
string tableName = GetClassName(typeof(T));
DataTable dt = new DataTable();
string fieldName;
object fieldValue;
string[] fieldNames;
System.Reflection.PropertyInfo[] Props = typeof(T).GetProperties();
fieldNames = new string[Props.Length];
for (int i = 0; i < Props.Length; i++)
{
fieldName = Props[i].Name;
fieldNames[i] = fieldName;
dt.Columns.Add(fieldName, Props[i].PropertyType);
}
for (int r = 0; r < oList.Count; r++)
{
DataRow dr = dt.NewRow();
T o = oList[r];
for (int i = 0; i < fieldNames.Length; i++)
{
fieldValue = Props[i].GetValue(o, null);
dr[fieldNames[i]] = fieldValue;
}
dt.Rows.Add(dr);
}
return dt;
}
catch(Exception ex) { }
return null;
}
}
List<T> 转换 DataTable的更多相关文章
- dataGrid转换dataTable
#region dataGrid转换dataTable /// <summary> /// dataGrid转换dataTable /// </summary> ...
- C# 将Excel以文件流转换DataTable
/* *引用 NuGet包 Spire.XLS */ /// <summary> /// Excel帮助类 /// </summary> public class ExcelH ...
- List转换DataTable
/// <summary> /// 将泛类型集合List类转换成DataTable /// </summary> /// <param name="list&q ...
- DataRow数组转换DataTable
public DataTable ToDataTable(DataRow[] rows) { if (rows == null || rows.Length == 0) return null; Da ...
- C# DataRow[]转换DataTable
DataTable dt = ... DataRow[] dr = dt.Select("ID=14"); dt = dr.CopyToDataTable();
- c# 将csv文件转换datatable的两种方式。
第一种: public static DataTable csvdatatable(string path) { DataTable dt = new DataTable(); string conn ...
- 实体lis<T>t转换datatable
public static DataTable ListToTable<T>(List<T> list) { Type type = typeof(T) ...
- 读CSV转换datatable
using System.Data; using System.IO; /// <summary> /// Stream读取.csv文件 /// </summary> // ...
- SqL读取XML、解析XML、SqL将XML转换DataTable、SqL将XML转换表
DECLARE @ItemMessage XML )) SET @ItemMessage=N' <ReceivablesInfos> <ReceivablesList> < ...
随机推荐
- iOS -- 神战
http://github.ibireme.com/github/list/ios/# https://github.com/Tim9Liu9/TimLiu-iOS http://www.ios122 ...
- linux定时任务生产java服务无法执行问题案例
我写了一个重启resin的脚本,由于业务原因,需要定时在某一个时间重启下resin服务器 于是就在crontab里配置了如下内容: * * - root /usr/local/bin/resin_re ...
- springMvc接受日期类型参数处理
这个问题,也即是springMvc如何进行参数类型的转换 以把client传过来一个String类型,转换为日期类型为例: 1.controller /** * 接收日期类型参数 * 注意: * sp ...
- Ehcache缓存配置
Cache的配置很灵活,官方提供的Cache配置方式有好几种.你可以通过声明配置.在xml中配置.在程序里配置或者调用构造方法时传入不同的参数. 你可以将Cache的配置从代码中剥离出来,也可以在使用 ...
- Ajax工作原理(转)
1.ajax技术的背景 不可否认,ajax技术的流行得益于google的大力推广,正是由于google earth.google suggest以及gmail等对ajax技术的广泛应用,催生了ajax ...
- apache ab压力测试
今天提到压力测试,想起以前看到的ab,于是又重新查找了下资料,并记录了下. ab命令会创建很多的并发访问线程,模拟多个访问者同时对某一URL地址进行访问. 它的测试目标是基于URL的,因此,既可以用来 ...
- extract()函数
extract(),它的主要作用是将数组展开,键名作为变量名,元素值为变量值 extract($_POST);//相当于$username = $_POST['username'];//$passwo ...
- NSTimer内存泄漏导致控制器不调用dealloc
创建定时器会在一定的间隔后执行某些操作,一般大家会这样创建定时器,这样创建的定时,self对定时器有个引用,定时器对self也有个引用,造成了循环引用,最终造成了内存泄漏,如果定时器在做下载的操作就会 ...
- BitmapFactory
1.以文件流的方式,假设在sdcard下有test.png图片FileInputStream fis = newFileInputStream("/sdcard/test.png" ...
- bzoj4404: [Neerc2015]Binary vs Decimal
WC结束了,来补一下这题的题解 首先感谢SC神犇YYY(第一个AC此题的神犇)教我做法 再感谢教YYY做法的Claris大爷 首先,我们发现一个性质,一个合法的数的后缀必定是合法的,所以我们就可以bf ...