/// <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. 两台Linux之间传文件

    安装sudo apt-get install openssh-client openssh-server 使用scp命令: scp john@~/hallo.h /usr/include 将左边移动到 ...

  2. OpenStack概念架构简述

    什么是OpenStack OpenStack既是一个社区,也是一个项目和一个开源软件,它提供了一个部署云的操作平台或工具集.其宗旨在于,帮助组织运行为虚拟计算或存储服务的云,为公有云.私有云,也为大云 ...

  3. 遍历XML文件

    #encoding=utf-8 from xml.etree import ElementTree as ET #要找出所有人的年龄 per=ET.parse('d:\\1.xml') p=per.f ...

  4. [SoapUI] 通过context获取response并解析里面的某个字段的值

    import com.eviware.soapui.support.GroovyUtils def groovyUtils = new GroovyUtils( context ) def realI ...

  5. linux 常用压缩打包和解压命令

    ## zcvf gzip jcvf bzip2 gunzip  tar zxvf  jxvf  

  6. vertical-align和text-align

    vertical-align只适用于内联元素. 垂直对齐:vertical-align属性(转) 行高与单行纯文字的垂直居中,而如果行内含有图片和文字,在浏览器内浏览时,读者可以发现文字和图片在垂直方 ...

  7. window.load 和$(document).ready() 、window.load和body onload区别

    1.执行时间 window.onload必须等到页面内包括图片的所有元素加载完毕后才能执行. $(document).ready()是DOM结构绘制完毕后就执行,不必等到加载完毕.2.编写个数不同 w ...

  8. ST3 插件和技巧

    Emmet插件: 快速生成HTML文档结构, 快速编写跨浏览器的CSS并自动帮助你同时编辑, 强大! 语法技巧 简单实用, 值得掌握! SideBar Enhancements插件:  改进了侧边栏, ...

  9. centos6.8下redis的安装和配置

    centos6.8下redis的安装和配置 下载.安装 在redis官网可以获取到最新版本的redis 进入/usr/local/目录,执行如下命令 wget http://download.redi ...

  10. KBMMW 4.80.00 发布

    一大波更新来了. 4.80.00 March 30 2015 Important notes (changes that may break existing code)        ======= ...