/// <summary>
/// 是否为空...
/// </summary>
/// <param name="str">数据值</param>
/// <param name="typeName">数据类型</param>
/// <returns></returns>
public static object ChekIsNullOrEmpty(object str,string typeName)
{
switch (typeName)
{
case "String":
return str == null ? "" : str.ToString();
case "DateTime":
return str == null || str.ToString() == "0001/1/1 0:00:00" ? DateTime.Parse("1990-1-1") : str;
default:
return str == null ? "" : str.ToString();
}
}

  

 /// <summary>
/// 给实体类空值赋值
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="model">实体类</param>
/// <returns></returns>
public static T ChekModelIsNullOrEmpty<T>(T model)
{
foreach (PropertyInfo property in typeof(T).GetProperties())
{
object strValue = DataHelper.ChekIsNullOrEmpty(model.GetType().GetProperty(property.Name).GetValue(model, null), property.PropertyType.Name);
model.GetType().GetProperty(property.Name).SetValue(model, strValue, null);
}
return model;
}

  

/// <summary>
/// 将一个列表转换成DataTable,如果列表为空将返回空的DataTable结构
/// </summary>
/// <typeparam name="T">要转换的数据类型</typeparam>
/// <param name="entityList">实体对象列表</param>
public static DataTable EntityListToDataTable<T>(List<T> entityList)
{
DataTable dt = new DataTable(); //取类型T所有Propertie
Type entityType = typeof(T);
PropertyInfo[] entityProperties = entityType.GetProperties();
Type colType = null;
foreach (PropertyInfo propInfo in entityProperties)
{ if (propInfo.PropertyType.IsGenericType)
{
colType = Nullable.GetUnderlyingType(propInfo.PropertyType);
}
else
{
colType = propInfo.PropertyType;
} if (colType.FullName.StartsWith("System"))
{
dt.Columns.Add(propInfo.Name, colType);
}
} if (entityList != null && entityList.Count > 0)
{
foreach (T entity in entityList)
{
DataRow newRow = dt.NewRow();
foreach (PropertyInfo propInfo in entityProperties)
{
if (dt.Columns.Contains(propInfo.Name))
{
object objValue = propInfo.GetValue(entity, null);
newRow[propInfo.Name] = objValue == null ? DBNull.Value : objValue;
}
}
dt.Rows.Add(newRow);
}
} return dt;
}

  

/// <summary>
/// 将一个DataTable转换成列表
/// </summary>
/// <typeparam name="T">实体对象的类型</typeparam>
/// <param name="dt">要转换的DataTable</param>
/// <returns></returns>
public static List<T> DataTableToEntityList<T>(DataTable dt)
{
List<T> entiyList = new List<T>(); Type entityType = typeof(T);
PropertyInfo[] entityProperties = entityType.GetProperties(); foreach (DataRow row in dt.Rows)
{
T entity = Activator.CreateInstance<T>(); foreach (PropertyInfo propInfo in entityProperties)
{
if (dt.Columns.Contains(propInfo.Name))
{
if (!row.IsNull(propInfo.Name))
{
propInfo.SetValue(entity, row[propInfo.Name], null);
}
}
} entiyList.Add(entity);
} return entiyList;
}

  

数据帮助类(DataHelper)的更多相关文章

  1. C#-ade.net-实体类、数据访问类

    实体类.数据访问类 是由封装演变而来,使对数据的访问更便捷,使用时只需要调用即可,无需再次编写代码 实体类是按照数据库表的结构封装起来的一个类 首先,新建文件夹 App_Code ,用于存放数据库类等 ...

  2. 我的DbHelper数据操作类

    其实,微软的企业库中有一个非常不错的数据操作类了.但是,不少公司(起码我遇到的几个...),对一些"封装"了些什么的东西不太敢用,虽然我推荐过微软的企业库框架了...但是还是要&q ...

  3. ado.net 实体类_数据访问类

    实体类: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ...

  4. 9_13学习完整修改和查询&&实体类,数据访问类

    完整修改和查询:中间变量运用. 1.先查 2.执行操作 ---------------------------------------------------- namespace ADO.NET_小 ...

  5. JavaScript 数据验证类

    JavaScript 数据验证类 /* JavaScript:验证类 author:杨波 date:20160323 1.用户名验证 2.密码验证 3.重复密码验证 4.邮箱验证 5.手机号验证 6. ...

  6. C# - DataValid数据验证类

    从EasyCode 摘取下来的数据验证类 using System; using System.Collections.Generic; using System.Text; namespace Le ...

  7. ADO.net 实体类 、数据访问类

    程序分三层:界面层.业务逻辑层.数据访问层 比较规范的写程序方法,要把业务逻辑层和数据访问层分开,此时需要创建实体类和数据访问类 实体类: 例 using System; using System.C ...

  8. ADO,NET 实体类 和 数据访问类

    啥也不说,看代码. --SQl中 --建立ren的数据库,插入一条信息 create database ren go use ren go create table xinxi ( code ) pr ...

  9. ADO.NET(完整修改和查询、实体类,数据访问类)

    一.完整修改和查询 在编写c#语句时需考虑到用户体验,例如在编写修改语句时,需要考虑到输入的内容在数据库中是否能够找到. 中间变量运用. 1.先查 2.执行操作 完整修改语句: bool has = ...

随机推荐

  1. windows server 2008 远程桌面连接数修改--无限连接

    1.开启远程桌面 我的电脑 |  属性 |  远程设置  |  远程 |  进允许运行使用网络级别身份验证的远程桌面的计算机连接(更安全)(N)

  2. unix架构

    UNIX Kernel(UNIX内核):指挥机器的运行,控制计算机的资源 UNIX Shell(UNIX外壳):是UNIX内核和用户的接口,是UNXI的命令解释器.目前常用的Shell有3种 Bour ...

  3. Control(拆点+最大流)

    Control http://acm.hdu.edu.cn/showproblem.php?pid=4289 Time Limit: 2000/1000 MS (Java/Others)    Mem ...

  4. 未能加载文件或程序集“AjaxControlToolkit”或它的某一个依赖项

    对于这个问题,网上的解答都大同小异,最多的就是Bin文件夹下没有dll文件,引用路径问题.但我碰到的问题偏偏不是这个,而是没有一个人给出方法的问题.其实问题很简单,也很低级:IIS上发布网站的时候把整 ...

  5. (一)ROS的安装与环境配置

    1.设置教程 1.1 打开system setting(系统设置)->Software&Updates(软件与更新) 1.2点击上方Other software(其他软件),点击左下角a ...

  6. memcache缓存失效

    缓存过期 memcached在处理过期的缓存项时,采用懒惰模式处理方法. 缓存项过期,不会立即删除,直到对该缓存项执行了get操作,才会删除过期缓存. > set key 0 10 > t ...

  7. Java中 Random

    Java中的Random()函数 (2013-01-24 21:01:04) 转载▼ 标签: java random 随机函数 杂谈 分类: Java 今天在做Java练习的时候注意到了Java里面的 ...

  8. Scrum 项目3.0--软件工程

    1. 确保product backlog井然有序.(参考示例图1) (例图1) 2.把故事进一步拆分成任务.(参考示例图2) (例图2) 3. 形成Sprint backlog. Scrum mast ...

  9. binlog怎样参与mysql recover的

    转自  Louis Hust's Blog MySQL两阶段提交 29 July 2015 参数介绍 两阶段提交 什么情况下会出现binlog写入了,但是实际这条数据不存在库中? 参数介绍 innod ...

  10. 网段;IP;广播地址;子网掩码;

    网段(network segment)一般指一个计算机网络中使用同一物理层设备(传输介质,中继器,集线器等)能够直接通讯的那一部分.例如,从192.168.0.1到192.168.255.255这之间 ...