C#DataTable与Model互转
/// <summary>
/// 实体转换辅助类
/// </summary>
public class ModelConvertHelper<T> where T : new()
{
/// <summary>
/// List泛型转换DataTable.
/// </summary>
public DataTable ListToDataTable<T>(List<T> items)
{
var tb = new DataTable(typeof(T).Name); PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo prop in props)
{
Type t = GetCoreType(prop.PropertyType);
tb.Columns.Add(prop.Name, t);
} foreach (T item in items)
{
var values = new object[props.Length]; for (int i = ; i < props.Length; i++)
{
values[i] = props[i].GetValue(item, null);
} tb.Rows.Add(values);
} return tb;
} /// <summary>
/// model转换DataTable
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="items"></param>
/// <returns></returns>
public DataTable ModelToDataTable<T>(T items)
{
var tb = new DataTable(typeof(T).Name); PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo prop in props)
{
Type t = GetCoreType(prop.PropertyType);
tb.Columns.Add(prop.Name, t);
} var values = new object[props.Length]; for (int i = ; i < props.Length; i++)
{
values[i] = props[i].GetValue(items, null);
} tb.Rows.Add(values); return tb;
} /// <summary>
/// Determine of specified type is nullable
/// </summary>
public static bool IsNullable(Type t)
{
return !t.IsValueType || (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>));
} /// <summary>
/// Return underlying type if type is Nullable otherwise return the type
/// </summary>
public static Type GetCoreType(Type t)
{
if (t != null && IsNullable(t))
{
if (!t.IsValueType)
{
return t;
}
else
{
return Nullable.GetUnderlyingType(t);
}
}
else
{
return t;
}
} /// <summary>
/// DataTable转换泛型List
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static List<T> DataTableToList(DataTable dt)
{
// 定义集合
List<T> ts = new List<T>(); // 获得此模型的类型
Type type = typeof(T);
string tempName = "";
foreach (DataRow dr in dt.Rows)
{
T t = new T();
// 获得此模型的公共属性
PropertyInfo[] propertys = t.GetType().GetProperties();
foreach (PropertyInfo pi in propertys)
{
tempName = pi.Name; // 检查DataTable是否包含此列 if (dt.Columns.Contains(tempName))
{
// 判断此属性是否有Setter
if (!pi.CanWrite) continue; object value = dr[tempName];
if (value != DBNull.Value)
pi.SetValue(t, value, null);
}
}
ts.Add(t);
}
return ts;
} public static T DataTableToModel(DataTable dt)
{
// 定义实体
T t = new T(); // 获得此模型的类型
Type type = typeof(T);
string tempName = ""; foreach (DataRow dr in dt.Rows)
{ // 获得此模型的公共属性
PropertyInfo[] propertys = t.GetType().GetProperties();
foreach (PropertyInfo pi in propertys)
{
tempName = pi.Name; // 检查DataTable是否包含此列 if (dt.Columns.Contains(tempName))
{
// 判断此属性是否有Setter
if (!pi.CanWrite) continue; object value = dr[tempName];
if (value != DBNull.Value)
pi.SetValue(t, value, null);
}
}
break;
}
return t;
}
}
C#DataTable与Model互转的更多相关文章
- DataTable转换为Model实体对象
记得在学校的时候,接触得最多的就是SqlHelper,每次在读取的时候不管是DataTable还是DataReader转换为实体对象的时候是最恼火的,因为要写很多代码,而且没有什么意义.后面接触到了反 ...
- DataTable转List,DataTable转为Model对象帮助类
DataTable转List,DataTable转为Model对象帮助类 public class ModelConvertHelper<T> where T : new() { publ ...
- json,DataTable,model
1.DataTable转json public class DataTableConvertJson { #region dataTable转换成Json格式 /// <summary> ...
- datatable 和实体互转
public static class ModelConvertHelper<T> where T : class,new() { public static List<T> ...
- List实体类、DataTable与xml互转
序列化常用Attribute讲解说明 [XmlRootAttribute("MyCity", Namespace="abc.abc", IsNullable=f ...
- C#把 DataTable转换为Model实体
public static List<T> GetModelFromDB<T>( DataTable dt ) { List<T> data = new List& ...
- 内裤:DataTable转Model
public class ConvertHelper<T> where T : new() { /// <summary> /// 利用反射和泛型 /// </summa ...
- DataSet,DataTable,XML格式互转
//// <summary> /// 将DataTable对象转换成XML字符串 /// </summary> /// <param name="dt" ...
- DataTable和List互转
/// <summary> /// list转datatable /// </summary> /// <typeparam name="T"> ...
随机推荐
- MongoDB整理笔记の指定命令和指定文件
MongoDB shell 不仅仅是一个交互式的shell,它也支持执行指定javascript 文件,也支持执行指定的命令片断.有了这个特性,就可以将MongoDB 与linux shell 完美结 ...
- 系统架构一:snmp+mrtg服务器监控
//@author:yuan<turing_zhy@163.com> 码字不易,转载请注明出处 #================================== 开始,服务器准备 ...
- 数据库索引与b+树
数据库索引详解 索引 当我们在设计数据库的时候,对表的一些属性有时会加上索引,但索引为什么能提高检索速率呢?是不是用了索引就一定可以提高效率呢?不同索引之间有什么区别呢?搞懂这些问题是灵活运用索引的必 ...
- webstorm激活服务器地址
2017.1.4版本可用 http://idea.imsxm.com/
- [jvm]java内存模型
一.java内存模型 Java虚拟机规范中试图定义一种Java内存模型(Java Memory Model,JMM)来屏蔽掉各种硬件和操作系统的内存访问差异,以实现让Java程序在各种平台下都能达到一 ...
- django中给ajax提交加上csrf
代码来自djangoproject网站 在html中的script标签下插入下面代码 在html文档加载时候运行下面代码,并且使用$.ajaxSetup设置ajax每次调用时候传入的数据,$.ajax ...
- 搭建Jenkins--(用于个人学习持续集成)
###1.安装java 完成后输入java -version,出现版本信息 输入 javac, 会出现 说明,JDK 和 JRE 安装正确 ###2.安装tomcat 1.下载Tomcat wind ...
- JDBC记录
13:55 2018/7/22 用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问 ---------常用API--------- |- Driver接口: 表示java驱动程序接 ...
- su切换用户报错cannot set user id: Resource temporarily unavailable
su: cannot set user id: 资源暂时不可用 登录root su - tomcat 报错: cannot set user id: Resource temporarily un ...
- 校园网络安全CTF 第一题 和 你真了解我吗?
第一题: 需要先找到相应头(REsponse header中的tips) <?php$flag = "***";if (isset($_GET['repo']))//检测变量 ...