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个库,其实原本该是一个库.先聊聊 ...
随机推荐
- jdk8与jdk7中hashMap的resize分析
在分析代码之前,我们先抛出下面的问题: hashmap 扩容时每个 entry 需要再计算一次 hash 吗? 我们首先看看jdk7中的hashmap的resize实现 1 void resize(i ...
- vue中使用vue-amap(高德地图)
因为项目要求调用高德地图,就按照官方文档按部就班的捣鼓,这一路上出了不少问题. 前言: vue-cli,node环境什么的自己安装设置推荐一个博客:https://blog.csdn.net/wula ...
- Flask+APScheduler定时任务
1.安装依赖 pip install flask_apscheduler 2.使用定时任务 ```python from flask import Flask from flask_apschedul ...
- jdk1.8 HashMap底层数据结构:深入解析为什么jdk1.8 HashMap的容量一定要是2的n次幂
前言 1.本文根据jdk1.8源码来分析HashMap的容量取值问题: 2.本文有做 jdk1.8 HashMap.resize()扩容方法的源码解析:见下文“一.3.扩容:同样需要保证扩容后的容量是 ...
- 基于hprose-golang创建RPC微服务
Hprose(High Performance Remote Object Service Engine) 是一款先进的轻量级.跨语言.跨平台.无侵入式.高性能动态远程对象调用引擎库.它不仅简单易用, ...
- 机器学习中的误差 Where does error come from?
误差来自于偏差和方差(bias and variance) 对于随机变量 X,假设其期望和方差分别为 μ 和 σ2.随机采样 N 个随机变量构成样本,计算算术平均值 m,并不会直接得到 μ (除非 ...
- Spark 系列(十四)—— Spark Streaming 基本操作
一.案例引入 这里先引入一个基本的案例来演示流的创建:获取指定端口上的数据并进行词频统计.项目依赖和代码实现如下: <dependency> <groupId>org.apac ...
- 谈谈你对java平台的理解?
问题:谈谈你对java平台的理解?java是解释执行,这句话对吗? 典型回答:java本身是一种面向对象的语言,具有很好的跨平台的能力,能够做到“write once ,run anywhere”.另 ...
- Codeforces 868E Policeman and a Tree
题意简述 给你一颗有n个点的树,每条边有边权,有一个警察一开始在点S,他的速度是1,即通过一条长度为x的边要花x单位时间. 有m个罪犯,一开始第i个在点x[i],他们的速度无限快. 如果罪犯和警察到达 ...
- 洛谷 P1960 列队
题意简述 有一个n × m 的矩阵,第i行第j列元素编号为(i - 1)× m +j 每次将一个数取出,其他元素依次向左,向上填补空缺,最后将取出的数放入矩阵最后一格 求每次取出数的编号 题解思路 由 ...