当前位置:网站首页> 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简化数据库访问的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持软件开发网。
边栏推荐
- 有人碰到过这种情况吗,oracle logminer 同步的时候,clob字段的值丢失
- Meta再放大招!VR新模型登CVPR Oral:像人一样「读」懂语音
- PG基础篇--逻辑结构管理(触发器)
- nexus搭建npm依赖私库
- ustime写出了bug
- 晓看天色暮看云,美图欣赏
- Fiori applications are shared through the enhancement of adaptation project
- Introduction to reverse debugging PE structure input table output table 05/07
- Vs code setting Click to open a new file window without overwriting the previous window
- localtime居然不可重入,踩坑了
猜你喜欢

mysql统计账单信息(下):数据导入及查询

The future of game guild in decentralized games
![leetcode:241. Design priority for operation expression [DFS + Eval]](/img/d0/8dedeba7ecedccd25e0e3e96ff3362.png)
leetcode:241. Design priority for operation expression [DFS + Eval]

软件测试中功能测试流程

【历史上的今天】7 月 1 日:分时系统之父诞生;支付宝推出条码支付;世界上第一支电视广告

Feign & Eureka & zuul & hystrix process

Fundamentals of number theory and its code implementation
![[brain opening] west tide and going to the world series](/img/b2/444af296e170d19629800b3d4c50fa.jpg)
[brain opening] west tide and going to the world series

VM虚拟机配置动态ip和静态ip访问

Fiori applications are shared through the enhancement of adaptation project
随机推荐
数字化转型再下一城,数字孪生厂商优锘科技宣布完成超3亿元融资
Development trend and market demand analysis report of China's high purity copper industry Ⓕ 2022 ~ 2028
用.Net Core接入微信公众号开发
Localtime can't re-enter. It's a pit
be based on. NETCORE development blog project starblog - (13) add friendship link function
软件测试中功能测试流程
题目 2612: 蓝桥杯2021年第十二届省赛真题-最少砝码(枚举找规律+递推)
彩色五角星SVG动态网页背景js特效
Interpretation of hard threshold function [easy to understand]
Based on the open source stream batch integrated data synchronization engine Chunjun data restore DDL parsing module actual combat sharing
使用nvm管理nodejs(把高版本降级为低版本)
VM virtual machine configuration dynamic IP and static IP access
codeforces -- 4B. Before an Exam
Manage nodejs with NVM (downgrade the high version to the low version)
请问flink mysql cdc 全量读取mysql某个表数据,对原始的mysql数据库有影响吗
SQLAlchemy在删除有外键约束的记录时,外键约束未起作用,何解?
王兴的无限游戏迎来“终极”一战
VS Code 设置代码自动保存
基因检测,如何帮助患者对抗疾病?
leetcode:241. 为运算表达式设计优先级【dfs + eval】