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转换的更多相关文章

  1. 通过.net反射技术实现DataReader转换成Model实体类列表

     public static T ReaderToModel<T>(IDataReader dr) { try {  using (dr) {  if (dr.Read()) {  Typ ...

  2. c#将List转换成DataTable(采用Emit)

    前段时间通过网上查找,使用emit将Datatable,DataReader转换成List<T>了.这是从数据库到展示. 但是最近整理Hikari(我写的数据库连接池),发现c#里面数据库 ...

  3. linq世界走一走(LINQ TO SQL)

    前言:作为linq的一个组件,同时作为ADO.NET的一个组成部分,LINQ TO SQL提供了将关系数据映射为对象的运行时基础结构. LINQ TO SQL是通过将关系数据库对象的数据模型(如一个数 ...

  4. c# 轻量级ORM框架 实现(一)

    发布一个自己写的一个轻量级ORM框架,本框架设计期初基于三层架构.所以从命名上来看,了解三层的朋友会很好理解. 设计该框架的目的:不想重复的写增删改查,把精力放到功能实现上. 发布改框架的原因:希望给 ...

  5. 利用npoi把多个DataTable导入Excel多个sheet中

    { 题外拓展:把datatable插入dataset DataTable fuben = new DataTable();//定义的datatablefuben = table.Tables[0].C ...

  6. 反射实现 Data To Model

    调用 : public ActionResult Index() { DataTable dt = new DataTable(); dt.Columns.Add("Name"); ...

  7. MVC仓储类Repository

    接口: using Common; using System; using System.Collections; using System.Collections.Generic; using Sy ...

  8. C#通用数据访问类库

    说明:此篇文章是给那些和我一样仍在使用ADO.NET访问数据库的.NET开发人员写的,因为某些原因,比如还在使用.NET3.0以下版本开发.NET应用或者所使用的数据库对ORM支持不是很好,或者是对O ...

  9. c#随便聊聊数据库操作

    最近在学习web后台以及Python,到了程序员的转折年纪了,哎.估计很久不会写博文了.言归正传. 在原理的数据库连接池HiKari项目上.我扩展了独立的3个库,说是3个库,其实原本该是一个库.先聊聊 ...

随机推荐

  1. Windows 下配置 Vagrant 环境

    Vagrant是一个基于 Ruby 的工具,用于创建和部署虚拟化开发环境.它使用 Oracle 的开源VirtualBox虚拟化系统. Vagrant 在快速搭建开发环境方面是很赞的,试想一个团队中, ...

  2. Redis进阶应用:Redis+Lua脚本实现复合操作

    一.引言 Redis是高性能的key-value数据库,在很大程度克服了memcached这类key/value存储的不足,在部分场景下,是对关系数据库的良好补充.得益于超高性能和丰富的数据结构,Re ...

  3. 【原创】TextCNN原理详解(一)

    ​ 最近一直在研究textCNN算法,准备写一个系列,每周更新一篇,大致包括以下内容: TextCNN基本原理和优劣势 TextCNN代码详解(附Github链接) TextCNN模型实践迭代经验总结 ...

  4. Java ActionListenner类的一些理解

    Java的ActionListenner事实上我去年年这个时候大概就已经接触到了,也学会了比较简单的使用.但却始终不能理解ActionListenner的一系列的运行是怎么维持这么一个联系的? 我产生 ...

  5. 前端登录jq图形验证码

    <!DOCTYPE html><html lang="zh"><head> <meta charset="UTF-8" ...

  6. java秒杀系列(2)- 页面静态化技术

    前言 通过代码片段分别介绍服务端渲染.客户端渲染.对象缓存三种方式的写法. 代码片段仅供参考,具体实现需要根据业务场景自行适配,但思想都是一样. 一.服务端渲染方式 1.接口返回html页面的设置 @ ...

  7. 神经网络优化算法:Dropout、梯度消失/爆炸、Adam优化算法,一篇就够了!

    1. 训练误差和泛化误差 机器学习模型在训练数据集和测试数据集上的表现.如果你改变过实验中的模型结构或者超参数,你也许发现了:当模型在训练数据集上更准确时,它在测试数据集上却不⼀定更准确.这是为什么呢 ...

  8. github的详细使用,非常简单!

    https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/

  9. Sqlserver 查询分组 记录

    select b.* from (select a.*,row_number() over (partition by 列1 order by 列2 desc) rn from a) b ; --如需 ...

  10. 性能测试学习第四天-----loadrunner:jdbc批量制造测试数据 & controller应用

    Javavuser协议 1.过程概述:在eclipse中用java编写sql执行脚本,复制到lr中,调整后通过参数化迭代批量制造测试数据: 2.步骤: 1).在eclipse中新建java proje ...