创建CreatDataTable
来源:互联网 发布:免费申请三级域名网站 编辑:程序博客网 时间:2024/05/25 19:56
public static DataTable CreateDataTable<T>(this IEnumerable<T> arry)
{
Type type = typeof(T);
var ret = new DataTable();
ret.TableName = type.Name;
foreach (PropertyInfo item in type.GetProperties())
{
if (item.PropertyType.FullName.Contains("Nullable") && item.PropertyType.FullName.Contains("Int32"))
ret.Columns.Add(new DataColumn(item.Name, typeof(int)));
else if (item.PropertyType.FullName.Contains("Nullable") && item.PropertyType.FullName.Contains("Decimal"))
ret.Columns.Add(new DataColumn(item.Name, typeof(decimal)));
else if (item.PropertyType.FullName.Contains("Nullable") && item.PropertyType.FullName.Contains("Boolean"))
ret.Columns.Add(new DataColumn(item.Name, typeof(bool)));
else if (item.PropertyType.FullName.Contains("Nullable") && item.PropertyType.FullName.Contains("DateTime"))
ret.Columns.Add(new DataColumn(item.Name, typeof(DateTime)));
else
ret.Columns.Add(new DataColumn(item.Name, item.PropertyType));
}
foreach (T item in arry)
{
var Row = ret.NewRow();
foreach (PropertyInfo pi in type.GetProperties())
{
if (pi.GetValue(item, null) == null)
{
Row[pi.Name] = DBNull.Value;
}
else
{
Row[pi.Name] = pi.GetValue(item, null);
}
}
ret.Rows.Add(Row);
}
return ret;
}
public static DataTable CreateDataTable<T>(this IEnumerable<T> arry, string tbName)
{
Type type = typeof(T);
var ret = new DataTable();
ret.TableName = tbName;
foreach (PropertyInfo item in type.GetProperties())
{
if (item.PropertyType.FullName.Contains("Nullable") && item.PropertyType.FullName.Contains("Int32"))
ret.Columns.Add(new DataColumn(item.Name, typeof(int)));
else if (item.PropertyType.FullName.Contains("Nullable") && item.PropertyType.FullName.Contains("Decimal"))
ret.Columns.Add(new DataColumn(item.Name, typeof(decimal)));
else if (item.PropertyType.FullName.Contains("Nullable") && item.PropertyType.FullName.Contains("Boolean"))
ret.Columns.Add(new DataColumn(item.Name, typeof(bool)));
else if (item.PropertyType.FullName.Contains("Nullable") && item.PropertyType.FullName.Contains("DateTime"))
ret.Columns.Add(new DataColumn(item.Name, typeof(DateTime)));
else
ret.Columns.Add(new DataColumn(item.Name, item.PropertyType));
}
foreach (T item in arry)
{
var Row = ret.NewRow();
foreach (PropertyInfo pi in type.GetProperties())
{
if (pi.GetValue(item, null) == null)
{
Row[pi.Name] = DBNull.Value;
}
else
{
Row[pi.Name] = pi.GetValue(item, null);
}
}
ret.Rows.Add(Row);
}
return ret;
}
}
}
- 创建CreatDataTable
- 创建
- 创建
- 创建数据库、创建表
- db2 创建实例,创建数据库,创建表
- 创建主键 创建外键 创建约束
- java 创建文件-创建目录-创建临时文件
- 创建项目
- 自动创建
- 创建同义词
- 创建分区表
- 创建虚拟目录
- 创建触发器
- 创建触发器
- blog创建
- 创建控制台
- 创建控制台
- 博客创建
- 写在开始
- Oracle数据分组:group by,having,rollup,cube,grouping sets
- php相隔算天数
- 足疗健身祛病歌
- 由简到难生成数据库报表(三)
- 创建CreatDataTable
- linux 下解决pdf 乱码
- 足疗师手把手教你家中做足疗
- 4月
- 软件质量控制实践――Microsoft 篇 (1)
- ArcGIS Runtime 本地API介绍(一)
- 最近
- 路径2
- pcb电路板中同时存在高压和低压时布线需要注意的几点