数据帮助类(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 = ...
随机推荐
- java.lang.Error: Unresolved compilation problem: 解决方案
严重: Allocate exception for servlet WX_Interfacejava.lang.Error: Unresolved compilation problem: The ...
- 获取客户端真实IP地址
Java-Web获取客户端真实IP: 发生的场景:服务器端接收客户端请求的时候,一般需要进行签名验证,客户端IP限定等情况,在进行客户端IP限定的时候,需要首先获取该真实的IP. 一般分为两种情况: ...
- ftp使用(图文详解)
41,准备: FileZilla_Server-v0.9.41.rar 2.安装,可以全部默认下一步 这里装在了E盘 3安装成功添加用户 添加用户名yanan 选择share folders选择要共享 ...
- 在不用重做系统下,把硬盘IDE模式修改为AHCI模式
Win10自2015年7月29日诞生以来已经3年多了,虽然截至到现在Win7用户使用者仍然比Win10用户多,但是Win10用户也在逐渐增加 所使用的硬件--硬盘 用户为了更好的体验,也逐步的从H ...
- nginx反向代理部署与演示(二)
我们把LB01作为负载均衡器,WEB01和WEB02作为两台web服务器. WEB01与WEB02虚拟主机配置如下: 我们修改nginx下的conf/nginx.conf文件,在http{}中 ...
- Golang之http编程
Go原生支持http.import("net/http") Go的http服务性能和nginx比较接近 几行代码就可以实现一个web服务 服务端http package main ...
- POJ 3709 K-Anonymous Sequence - 斜率优化dp
描述 给定一个数列 $a$, 分成若干段,每段至少有$k$个数, 将每段中的数减少至所有数都相同, 求最小的变化量 题解 易得到状态转移方程 $F_i = \min(F_j + sum_i - su ...
- inline 引起undefined reference to
main.cc:57: undefined reference to `evpp::udp::UdpDecoder::GetHeader()'collect2: error: ld returned ...
- cxf soap rest webservice spring
1. 导入 jar 包 2. 编写接口 3. 编写实现 4. 配置spring 配置文件 5. 配置web.xml servlet 6. 访问 package com.diancai.test; im ...
- android 混淆文件proguard.cfg详解 (转载)
-injars androidtest.jar[jar包所在地址] -outjars out[输出地址] -libraryjars 'D:\android-sdk-windows\platf ...