数据帮助类(DataHelper)
/// <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)的更多相关文章
- C#-ade.net-实体类、数据访问类
实体类.数据访问类 是由封装演变而来,使对数据的访问更便捷,使用时只需要调用即可,无需再次编写代码 实体类是按照数据库表的结构封装起来的一个类 首先,新建文件夹 App_Code ,用于存放数据库类等 ...
- 我的DbHelper数据操作类
其实,微软的企业库中有一个非常不错的数据操作类了.但是,不少公司(起码我遇到的几个...),对一些"封装"了些什么的东西不太敢用,虽然我推荐过微软的企业库框架了...但是还是要&q ...
- ado.net 实体类_数据访问类
实体类: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ...
- 9_13学习完整修改和查询&&实体类,数据访问类
完整修改和查询:中间变量运用. 1.先查 2.执行操作 ---------------------------------------------------- namespace ADO.NET_小 ...
- JavaScript 数据验证类
JavaScript 数据验证类 /* JavaScript:验证类 author:杨波 date:20160323 1.用户名验证 2.密码验证 3.重复密码验证 4.邮箱验证 5.手机号验证 6. ...
- C# - DataValid数据验证类
从EasyCode 摘取下来的数据验证类 using System; using System.Collections.Generic; using System.Text; namespace Le ...
- ADO.net 实体类 、数据访问类
程序分三层:界面层.业务逻辑层.数据访问层 比较规范的写程序方法,要把业务逻辑层和数据访问层分开,此时需要创建实体类和数据访问类 实体类: 例 using System; using System.C ...
- ADO,NET 实体类 和 数据访问类
啥也不说,看代码. --SQl中 --建立ren的数据库,插入一条信息 create database ren go use ren go create table xinxi ( code ) pr ...
- ADO.NET(完整修改和查询、实体类,数据访问类)
一.完整修改和查询 在编写c#语句时需考虑到用户体验,例如在编写修改语句时,需要考虑到输入的内容在数据库中是否能够找到. 中间变量运用. 1.先查 2.执行操作 完整修改语句: bool has = ...
随机推荐
- luoguP3367 [模板]并查集
题目链接:https://www.luogu.org/problemnew/show/P3367 思路: 今天学了新算法——并查集,本题是简单的并查集题的模板. 核心思想是“递归+压缩路径”. 并查集 ...
- hdoj1251-统计难题 【字典树】
http://acm.hdu.edu.cn/showproblem.php?pid=1251 统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory ...
- java程序员从ThinkPad到Mac的使用习惯改变
https://blog.csdn.net/yczz/article/details/49993417
- 在Objc项目中调用Swift
之前的文字中记录了在Swift项目中调用OC的相关代码,比较简单直接 传送门 但是在OC中调用swift代码则不是那么的和谐,网络上很多文章业已经有点陈旧.记录步骤如下: 1.创建OC项目 (1)启动 ...
- 最近公共祖先 · Lowest Common Ancestor
[抄题]: Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. “Th ...
- php的静态化
原理,将动态的页面,存储为静态的HTML静态页,使浏览器直接请求该静态页. 测试:一个PHP动态页面与一个静态页面所消耗的时间 一般可以使用apache自带的ab(apache bench)程序来测试 ...
- Spring框架的AOP的底层实现
1. Srping框架的AOP技术底层也是采用的代理技术,代理的方式提供了两种 1. 基于JDK的动态代理 * 必须是面向接口的,只有实现了具体接口的类才能生成代理对象 2. 基于CGLIB动态代理 ...
- DB2中的数据类型
DB2中的数据类型DB2内置数据类型可以分成数值型(numeric).字符串型(character string).图形字符串(graphic string).二进制字符串型(binary strin ...
- 神奇的照片修复术,这才是 PS 的正确打开方式!
蒲公英种子从远处飘回 聚成伞的模样 太阳从西边升起 落向东方 运动员回到起跑线上 轰鸣的火车退回家乡 雪花纷飞 飘向天际 我沉入梦乡 你还在我身旁 ——公益广告 大概只有时光倒流,我们才能回到那些每天 ...
- Laravel中用GuzzleHttp
阅读数:14715 今天项目中用到GuzzleHttp,开始不知道怎么用,其实还是很简单的. 直接在项目根目录,输入以下命令 composer require guzzlehttp/guzzle 1 ...