DataReader转换
public static partial class Extension
{ private static ConcurrentDictionary<Type, ConcurrentDictionary<string, PropertyInfo>> typePropertyCache = new ConcurrentDictionary<Type, ConcurrentDictionary<string, PropertyInfo>>(); private static ConcurrentDictionary<string, PropertyInfo> GetTypePropertyMap(Type entityType)
{
if (typePropertyCache.ContainsKey(entityType))
return typePropertyCache[entityType];
ConcurrentDictionary<string, PropertyInfo> propertyMappers = new ConcurrentDictionary<string, PropertyInfo>();
var properties = entityType.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty);
foreach (var item in properties)
{
string columnName = ImprovedNamingStrategy.Instance.PropertyToColumnName(item.Name);
propertyMappers.TryAdd(columnName, item);
}
typePropertyCache[entityType] = propertyMappers;
return propertyMappers;
} public static IEnumerable<T> QueryList<T>(this IDbConnection connection, string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
{
IDataReader reader = connection.ExecuteReader(sql, param, transaction, commandTimeout, commandType); List<T> list = new List<T>();
var propertyMappers = GetTypePropertyMap(typeof(T));
while (reader.Read())
{
T obj = Activator.CreateInstance<T>(); for (int i = ; i < reader.FieldCount; i++)
{
if (reader.IsDBNull(i))
{
continue;
}
string columnName = reader.GetName(i).ToLower();
if (!propertyMappers.ContainsKey(columnName))
{
continue;
}
PropertyInfo property = propertyMappers[columnName];
if (property == null)
{
continue;
} property.SetValue(obj, ChangeType(reader[columnName], property.PropertyType));
}
list.Add(obj);
}
return list;
} static public object ChangeType(object value, Type type)
{
if (value == null && type.IsGenericType) return Activator.CreateInstance(type);
if (value == null) return null;
if (type == value.GetType()) return value;
if (type.IsEnum)
{
if (value is string)
return Enum.Parse(type, value as string);
else
return Enum.ToObject(type, value);
}
if (!type.IsInterface && type.IsGenericType)
{
Type innerType = type.GetGenericArguments()[];
object innerValue = ChangeType(value, innerType);
return Activator.CreateInstance(type, new object[] { innerValue });
}
if (value is string && type == typeof(Guid)) return new Guid(value as string);
if (value is string && type == typeof(Version)) return new Version(value as string);
if (!(value is IConvertible)) return value;
return Convert.ChangeType(value, type);
}
}
DataReader转换的更多相关文章
- 通过.net反射技术实现DataReader转换成Model实体类列表
public static T ReaderToModel<T>(IDataReader dr) { try { using (dr) { if (dr.Read()) { Typ ...
- c#将List转换成DataTable(采用Emit)
前段时间通过网上查找,使用emit将Datatable,DataReader转换成List<T>了.这是从数据库到展示. 但是最近整理Hikari(我写的数据库连接池),发现c#里面数据库 ...
- linq世界走一走(LINQ TO SQL)
前言:作为linq的一个组件,同时作为ADO.NET的一个组成部分,LINQ TO SQL提供了将关系数据映射为对象的运行时基础结构. LINQ TO SQL是通过将关系数据库对象的数据模型(如一个数 ...
- c# 轻量级ORM框架 实现(一)
发布一个自己写的一个轻量级ORM框架,本框架设计期初基于三层架构.所以从命名上来看,了解三层的朋友会很好理解. 设计该框架的目的:不想重复的写增删改查,把精力放到功能实现上. 发布改框架的原因:希望给 ...
- 利用npoi把多个DataTable导入Excel多个sheet中
{ 题外拓展:把datatable插入dataset DataTable fuben = new DataTable();//定义的datatablefuben = table.Tables[0].C ...
- 反射实现 Data To Model
调用 : public ActionResult Index() { DataTable dt = new DataTable(); dt.Columns.Add("Name"); ...
- MVC仓储类Repository
接口: using Common; using System; using System.Collections; using System.Collections.Generic; using Sy ...
- C#通用数据访问类库
说明:此篇文章是给那些和我一样仍在使用ADO.NET访问数据库的.NET开发人员写的,因为某些原因,比如还在使用.NET3.0以下版本开发.NET应用或者所使用的数据库对ORM支持不是很好,或者是对O ...
- c#随便聊聊数据库操作
最近在学习web后台以及Python,到了程序员的转折年纪了,哎.估计很久不会写博文了.言归正传. 在原理的数据库连接池HiKari项目上.我扩展了独立的3个库,说是3个库,其实原本该是一个库.先聊聊 ...
随机推荐
- 手摸手,带你用vue实现后台管理权限系统及顶栏三级菜单显示
手摸手,带你用vue实现后台管理权限系统及顶栏三级菜单显示 效果演示地址 项目demo展示 重要功能总结 权限功能的实现 权限路由思路: 根据用户登录的roles信息与路由中配置的roles信息进行比 ...
- cogs 1254. 最难的任务 Dijkstra + 重边处理
1254. 最难的任务 ★ 输入文件:hardest.in 输出文件:hardest.out 简单对比时间限制:1 s 内存限制:128 MB [题目描述] 这个真的很难.算出 123 ...
- webgl核心要素
WebGL是一种3D绘图标准,这种绘图技术标准允许把JavaScript和OpenGL ES 2.0结合在一起,通过增加OpenGL ES 2.0的一个JavaScript绑定,提供硬件3D加速渲染, ...
- 使用excel计算骰子输赢概率
如何得到使用3个骰子掷赢4个骰子的概率(每个骰子的点数为1-6,点数一样算输) 分为3步解决: 第一步.计算n个骰子得到m点数的分布 1个骰子能得到1.2.3.4.5.6点数,每个点数出现的方式只有1 ...
- idea+Spring+Mybatis+jersey+jetty构建一个简单的web项目
一.先使用idea创建一个maven项目. 二.引入jar包,修改pom.xml <dependencies> <dependency> <groupId>org. ...
- Leader-Follower线程模型简介
参考58沈剑大神架构师之路上的文章,谈谈Leader-Follower线程模型: 上图就是L/F多线程模型的状态变迁点,共6个关键点: (1)线程有3种状态:领导leading,处理processin ...
- 从输入URL到浏览器显示页面发生了哪些事情---个人理解
经典面试题:从输入URL到页面显示发生了哪些事情 以前一直都记不住,这次自己理解了一下 用自己的话总结了一次,不对的地方希望大佬给我指出来 1.主机通过DHCP协议获取客户端的IP地址.子网掩码和DN ...
- 配置Windows Server 2008环境
上一章已经把Windows Server2008操作系统安装完毕,接下来配置一下Windows Server环境.配置网络和共享中心.配置桌面环境.配置用户IE设置.安装Telnet远程工具.配置文件 ...
- 《Java 8 in Action》Chapter 1:为什么要关心Java 8
自1998年 JDK 1.0(Java 1.0) 发布以来,Java 已经受到了学生.项目经理和程序员等一大批活跃用户的欢迎.这一语言极富活力,不断被用在大大小小的项目里.从 Java 1.1(199 ...
- 关于 .Net Core runtimeconfig 文件说明
在项目的bin\debug\netcoreapp${Version}下面能够找到这个${AppName}.runtimeconfig.json文件,简单来说,它就是用来定义用用程序所用的共享框架(.N ...