table 转实体
public class Table2Entity<T> where T : class,new()
{
public static List<T> GetEntitys(DataTable dt)
{
Dictionary<string, string> columns = new Dictionary<string, string>();
foreach (DataColumn item in dt.Columns)
{
columns.Add(item.ColumnName.Trim().ToLower(), item.ColumnName);
}
List<T> result = new List<T>();
var proptetis = typeof(T).GetProperties(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);
foreach (DataRow dr in dt.Rows)
{
T obj = new T();
foreach (var item in proptetis)
{
var attributes = item.GetCustomAttributes(typeof(RenameAttribute), true);
var columnName = (attributes != null && attributes.Length > ) ? ((RenameAttribute)(attributes[])).Name.Trim().ToLower() : item.Name.Trim().ToLower();
if (columns.Keys.Contains(columnName))
{
var mt = item.GetSetMethod(true);
var value = dr[columns[columnName]];
if (value == null || value == DBNull.Value || string.IsNullOrWhiteSpace(value.ToString())) continue;
if (item.PropertyType == typeof(string))
mt.Invoke(obj, new object[] { value.ToString() });
else if (item.PropertyType == typeof(int) || item.PropertyType == typeof(int?))
mt.Invoke(obj, new object[] { Convert.ToInt32(value) });
else if (item.PropertyType == typeof(long) || item.PropertyType == typeof(long?))
mt.Invoke(obj, new object[] { Convert.ToInt64(value) });
else if (item.PropertyType == typeof(double) || item.PropertyType == typeof(double?))
mt.Invoke(obj, new object[] { Convert.ToDouble(value) });
else if (item.PropertyType == typeof(decimal) || item.PropertyType == typeof(decimal?))
mt.Invoke(obj, new object[] { Convert.ToDecimal(value) });
else if (item.PropertyType == typeof(DateTime) || item.PropertyType == typeof(DateTime?))
mt.Invoke(obj, new object[] { Convert.ToDateTime(value) });
else if (item.PropertyType == typeof(float) || item.PropertyType == typeof(float?))
mt.Invoke(obj, new object[] { float.Parse(value.ToString()) });
else if (item.PropertyType == typeof(bool))
mt.Invoke(obj, new object[] { bool.Parse(value.ToString()) });
else if (item.PropertyType.BaseType == typeof(System.Enum))
{
mt.Invoke(obj, new object[] { Convert.ToInt32(value) });
}
}
}
result.Add(obj);
}
return result;
}
}
table 转实体的更多相关文章
- C# 泛型实现Table与实体的相互转换
public class ModelHandler<T> where T : new() { /// <summary> /// Table转换成实体 /// </sum ...
- @Entity 和 @Table
Java Persistence API定义了一种定义,可以将常规的普通Java对象(有时被称作POJO)映射到数据库.这些普通Java对象被称作Entity Bean.除了是用Java Persis ...
- JPA之@Entity、@Table、@Column、@Id
Java Persistence API定义了一种定义,可以将常规的普通Java对象(有时被称作POJO)映射到数据库.这些普通Java对象被称作Entity Bean.除了是用Java Persis ...
- JPA实体类中的注解
@Entity 标注于实体类上,通常和@Table是结合使用的,代表是该类是实体类@Table 标注于实体类上,表示该类映射到数据库中的表,没有指定名称的话就表示与数据库中表名为该类的简单类名的表名相 ...
- MyEclipse从数据库反向生成实体类通过Hibernate的方式----mysql数据库实例
1.我们通过DB与数据库建立连接 2.建立web工程,构建Hibernate框架 3.通过table生成实体类
- ToolsCodeTemplate使用
最近学习使用CodeSmith代码生成器 CodeSmith 是一种语法类似于asp.net的基于模板的代码生成器,程序可以自定义模板,从而减少重复编码的劳动量,提高效率. 作用:CodeSmith ...
- 7.DataAnnotations(数据注解)【Code-First 系列】
EF Code-First提供了一系列的数据注解的特性,你可以将其应用到你的领域类和属性中,数据注解属性重写了EF默认的约定. System.ComponentModel.DataAnnotation ...
- spring4+hibernate4+maven环境搭建
本文主要介绍利用maven搭建spring4+hibernate4开发环境. 首先我们创建一个maven项目,具体步骤就不详细介绍了,看看我们pom.xml文件 <project xmlns=& ...
- 详谈Hibernate框架关系映射!
接触Hibernate也有一小段的时间了,愈发的觉得Hibernate是个神奇的东西,为什么这么说呢?因为你可以不懂一行sql,直接面向对象,就可以将数据直接保存到数据库去!! 你还可以保存一个对象, ...
随机推荐
- 测试:safenet提供的CheckKey函数 内存泄漏。具体来说是句柄.
unsigned char vendor_code[] = "7XSQT4jxlSkDJhwqpxxfLwbuxgrYw93OMy+K5sc5pyfTa7HQo1ikLyg7FDuEpgUK ...
- PKU 1094 Sorting It All Out(拓扑排序)
题目大意:就是给定一组字母的大小关系判断他们是否能组成唯一的拓扑序列. 是典型的拓扑排序,但输出格式上确有三种形式: 1.该字母序列有序,并依次输出: 2.判断该序列是否唯一: 3.该序列字母次序之间 ...
- HDU 1114 Piggy-Bank(完全背包模板题)
完全背包模板题 #include<cstdio> #include<cstring> #include<algorithm> using namespace std ...
- 安卓 和 IOS 的icon 尺寸
安卓 36*36 48*48 72*72 96*96 IOS Icon.png – 57×57 iPhone (ios5/6) Icon@2x.png – 114×114 iPhone Retina ...
- TOSCA自动化测试工具--Log defect
1.执行完用例后,对于失败的用例进行分析,如果有缺陷,可以提对应的缺陷 2.在issues模块, 右键创建自己需要的文件夹,然后在文件夹上右键找到虫子图形点下,就可以创建缺陷了,填上对应的内容 3.如 ...
- Spring 事务管理tx,aop
spring tx:advice事务配置 2016年12月21日 17:27:22 阅读数:7629 http://www.cnblogs.com/rushoooooo/archive/2011/08 ...
- ANE报错fix:Could not generate timestamp: Connection reset.
如果你打包ANE时候 报了:Could not generate timestamp: Connection reset. 那么很有可能你用了JDK 1.8. 解决方案一 退回到 JDK 1.7,重新 ...
- 在MySQL中使用explain查询SQL的执行计划
1.什么是MySQL执行计划 要对执行计划有个比较好的理解,需要先对MySQL的基础结构及查询基本原理有简单的了解. MySQL本身的功能架构分为三个部分,分别是 应用层.逻辑层.物理层,不只是MyS ...
- kafka监控搭建
1. 下载 wget https://github.com/quantifind/KafkaOffsetMonitor/releases/download/v0.2.1/KafkaOffsetMoni ...
- nsis源码 nsisdialogdesigner
; 安装程序初始定义常量!define PRODUCT_NAME "nsisdialogdesigner"!define PRODUCT_VERSION "1.1.3&q ...