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,直接面向对象,就可以将数据直接保存到数据库去!! 你还可以保存一个对象, ...
随机推荐
- Java基础知识陷阱(八)
本文发表于本人博客. 这次我来说说关于&跟&&的区别,大家都知道&是位运算符,而&&是逻辑运算符,看下面代码: public static void m ...
- Git在Githib和Github上的使用
本文使用的环境是linux里 一.git的常用命令解释: 1.基础命令: git init #创建版本库 git add <file> #将文件修改添加到暂存区 git commit -m ...
- 持续(集成-->交付-->部署)
软件的开发工作的大致流程 编码 -> 构建 -> 集成 -> 测试 -> 交付 -> 部署 由上图可知「持续集成(Continuous Integration)」.「持续 ...
- pc端样式初始化
pc端样式初始化: /* http://meyerweb.com/eric/tools/css/reset/ /* http://meyerweb.com/eric/tools/css/reset/ ...
- XVII Open Cup named after E.V. Pankratiev Grand Prix of Moscow Workshops, Sunday, April 23, 2017 Problem D. Great Again
题目: Problem D. Great AgainInput file: standard inputOutput file: standard outputTime limit: 2 second ...
- C#如何获取枚举(Enum)变量的值
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Enum ...
- cocos2d: fullPathForFilename: No file found at /cc_2x2_white_image. Possible missing file.
程序运行的时候输出这条信息cocos2d: fullPathForFilename: No file found at /cc_2x2_white_image. Possible missing fi ...
- [2011-3-9 12:59 ]As3.0中的位图(Bitmap/BitmapData)用法
1.位图使用(模糊)滤镜 //创建一个矩形区域的BitmapData var bmd:BitmapData = new BitmapData(80, 30, false, 0xefefef); //画 ...
- 在Ubuntu中启动./jmeter-server报错Server failed to start: java.rmi.RemoteException: Cannot start. ranxf is a loopback address.解决方法
执行失败错误信息: root@ranxf:/home/ranxf/apache-jmeter-3.1/bin# ./jmeter-server Writing log file to: /home ...
- zookeeper和淘宝dubbo的关系
Dubbo建议使用Zookeeper作为服务的注册中心. 1. Zookeeper的作用: zookeeper用来注册服务和进行负载均衡,哪一个服务由哪一个机器来提供必需让调用者知 ...