当前位置:网站首页> Asp.netcore利用dynamic简化数据库访问
Asp.netcore利用dynamic简化数据库访问
2022-07-01 12:59:00 【1024问】
今天写了一个数据库的帮助类,代码如下。
public static class DbEx{ public static dynamic ReadToObject(this IDataReader reader) { var obj = new DbObject(); for (int i = 0; i < reader.FieldCount; i++) { obj[reader.GetName(i)] = new DbField() { DbData = reader[i] }; } return obj; } public class DbObject : DynamicObject { //自己实现一个,不用ExpandoObject, 以支持无视大小写读取 public override bool TryGetMember(GetMemberBinder binder, out object result) { result = this[binder.Name]; return true; } Dictionary<string, object> _values = new Dictionary<string, object>(StringComparer.CurrentCultureIgnoreCase); public object this[string index] { get => _values[index]; set => _values[index] = value; } } public class DbField { public object DbData { get; set; } public T Value<T>() { return (T)Convert.ChangeType(DbData, typeof(T)); } public static implicit operator string(DbField data) => data.Value<string>(); public static implicit operator int(DbField data) => data.Value<int>(); public static implicit operator DateTime(DbField data) => data.Value<DateTime>(); public static implicit operator double(DbField data) => data.Value<double>(); public static implicit operator bool(DbField data) => data.Value<bool>(); }}简单的来讲,可以把如下代码
GpsData parse(IDataReader reader){ return new GpsData() { IsValid = (bool)reader["IsValid"], Location = new Location () { Lon = (double)reader["Lon"], Lat = (double)reader["Lat"], }, Angle = (double)reader["Angle"], Speed = (double)reader["Speed"]), UpdateTime = (double)reader["Speed"]), };}转换为如下形式
GpsData parse(IDataReader reader){ var obj = reader.ReadToObject(); var state = new GpsData() { IsValid = obj.IsValid, Location = new Location() { Lon = obj.Lon, Lat = obj.Lat, }, Angle = obj.Angle, Speed = obj.Speed, UpdateTime = obj.UpdateTime, }; return state;}到此这篇关于Asp.net core利用dynamic简化数据库访问的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持软件开发网。
边栏推荐
- 数论基础及其代码实现
- VS Code 设置代码自动保存
- 晓看天色暮看云,美图欣赏
- ZABBIX 6.0 source code installation and ha configuration
- 【大型电商项目开发】性能压测-压力测试基本概念&JMeter-38
- nexus搭建npm依赖私库
- R language builds a binary classification model based on H2O package: using H2O GBM build gradient hoist model GBM, use H2O AUC value of AUC calculation model
- VM虚拟机配置动态ip和静态ip访问
- Function test process in software testing
- Operator-1 first acquaintance with operator
猜你喜欢

Function test process in software testing

Based on the open source stream batch integrated data synchronization engine Chunjun data restore DDL parsing module actual combat sharing

leetcode:226. 翻转二叉树【dfs翻转】

CS5268优势替代AG9321MCQ Typec多合一扩展坞方案

Shell script imports stored procedures into the database

79. 单词搜索【dfs + 回溯visit + 遍历起点】

数论基础及其代码实现

Operator-1 first acquaintance with operator

Zabbix 6.0 源码安装以及 HA 配置

工具箱之 IKVM.NET 项目新进展
随机推荐
I spent tens of thousands of dollars to learn and bring goods: I earned 3 yuan in three days, and the transaction depends on the bill
Will it affect the original MySQL database to read the data of a MySQL table in full by flick MySQL CDC
Fundamentals of number theory and its code implementation
How to count the status of network sockets?
【牛客刷题-SQL大厂面试真题】NO2.用户增长场景(某度信息流)
R language uses conf of yardstick package_ The mat function calculates the confusion matrix of the multiclass model on each fold of each cross validation (or resampling), and uses the summary to outpu
System test UI test summary and questions (interview)
华为HMS Core携手超图为三维GIS注入新动能
leetcode:329. The longest incremental path in the matrix [DFS + cache + no backtracking + elegance]
codeforces -- 4B. Before an Exam
Digital signal processing -- Design of linear phase (Ⅱ, Ⅳ) FIR filter (2)
硬件开发笔记(九): 硬件开发基本流程,制作一个USB转RS232的模块(八):创建asm1117-3.3V封装库并关联原理图元器件
题目 2612: 蓝桥杯2021年第十二届省赛真题-最少砝码(枚举找规律+递推)
List of QT players [easy to understand]
Vs code setting Click to open a new file window without overwriting the previous window
Ikvm of toolbox Net project new progress
项目部署,一点也不难!
有人碰到过这种情况吗,oracle logminer 同步的时候,clob字段的值丢失
基于开源流批一体数据同步引擎 ChunJun 数据还原 —DDL 解析模块的实战分享
localtime居然不可重入,踩坑了