DataTable转实体】的更多相关文章

之前一直在做WebForm的开发,数据绑定时直接DataTable绑定Gridview很方便,但是最近开始往MVC转,数据列表的传递和页面展示基本上是以List为主,像下面这样,遍历实体类的各个字段去赋值的办法当然是最浪费时间的. if (row["ID"] != null && row["ID"].ToString() != "") { model.bedID = int.Parse(row["ID"].To…
/// <summary> /// DataTable与实体类互相转换 /// </summary> /// <typeparam name="T">实体类</typeparam> public class ModelHandler<T> where T : new() { #region DataTable转换成实体类 /// <summary> /// 填充对象列表:用DataSet的第一个表填充实体类 ///…
在开发中可能会遇到这几种情况 1.EF或LINQ查询出来的匿名对象在其它地方调用不方便,又懒的手动建实体类 2.通过datatable反射实体需要先建一个类 ,头痛 3.通过SQL语句返回的实体也需要先建一个类 ,头痛 4.如果通过代码生成器要写模版,需要安装或者不想生成一堆不用的类 为了解决上面的不便之处,我封装了一个实体生成类,可以扔到程序里面任意调用 封装类: using System; using System.Collections.Generic; using System.Linq…
看了很多人的项目,很多都是用到三层架构,其中BLL层中有一种将DataTable转换为实体的方法.一直没有明白为啥要这样做,今天特意去搜索了一下,如果没有答案我是准备提问,寻求解答了.还好找到一个相关的网页.终于理解了,心中的一个困惑可以放下了. 好处(优点): 这样做的优点如下: |  编写B层的人员无需手动填写需要的字段,直接按一下点,全都提示出来了,想用哪个用哪个,不会出现写错的情况. |  不必了解数据库结构. |  符合面向对象思想. |  实体类的属性是强类型,每个字段的类型都是已知…
本案例提供了:把DataRow转换为单个实体.dataTable转换为List泛型支持时间格式转换. 下文的方法都是扩展方法.扩展方法要求写在静态类中,方法也要静态. 它必须在一个非嵌套.非泛型的静态类中 它至少要有一个参数 第一个参数必须加上this关键字作为前缀(第一个参数类型也称为扩展类型,即指方法对这个类型进行扩展) 第一个参数不能用其他任何修饰符(如不能使用ref out等修饰符) 第一个参数的类型不能是指针类型 1.将DataRow转换为实体 /// <summary> /// D…
多年前写的DataTable与实体类的转换,已放github 阅读目录 介绍 起因 代码 UnitTest GitHub 介绍 很多年前一直使用Ado.net,后来慢慢转型到其他的orm,在转型过程中,有意向将两者的模型结合起来,利用DataTable中的行状态完善一些mvc中的数据控制作用.现在把它放出来,留个纪念. 起因 很多年前,对Ado.net这块了解较深,当时公司也有一套框架,将Ado.net做成了ORMapping,所以,当时对DataTable的操作很是熟练. DataTable中…
因为Linq的查询功能很强大,所以从数据库中拿到的数据为了处理方便,我都会转换成实体集合List<T>. 开始用的是硬编码的方式,好理解,但通用性极低,下面是控件台中的代码: using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Demo1 { class Pr…
C# 中查询结果DataTable转实体类: 比如:List<RtmInterview> rtmList = GetDataById( id); public List<RtmInterview> GetDataById(string id) { List<RtmInterview> rtmList = new List<RtmInterview>(); bool ConnectionOpenHere = false; try { DataTable dt…
/// <summary> /// DataTable与实体类互相转换 /// </summary> /// <typeparam name="T">实体类</typeparam> public class ModelHandler<T> where T : new() { #region DataTable转换成实体类 /// <summary> /// 填充对象列表:用DataSet的第一个表填充实体类 ///…
原文地址:https://www.cnblogs.com/marblemm/p/7084797.html /// <summary> /// DataTable与实体类互相转换 /// </summary> /// <typeparam name="T">实体类</typeparam> public class ModelHandler<T> where T : new() { #region DataTable转换成实体类…
AutoMapper是一个.NET的对象映射工具. 项目地址:https://github.com/AutoMapper/AutoMapper. 帮助文档:https://github.com/AutoMapper/AutoMapper/wiki 主要用途 领域对象与DTO之间的转换.数据库查询结果映射至实体对象. 这里主要说下使用 AutoMapper 将 IDataReader.DataSet.DataTable 转为实体的方法. 依赖文件:AutoMapper.dll.AutoMapper…
using System.Runtime.Serialization; using System.Data; using System.Reflection; using System.Collections.Generic; namespace OrderSplit { /// <summary> /// DataTable与实体类互相转换 /// </summary> /// <typeparam name="T">实体类</typepar…
#region DataTable转换成实体类 /// <summary> /// 填充对象列表:用DataSet的第一个表填充实体类 /// </summary> /// <param name="ds">DataSet</param> /// <returns></returns> public List<T> FillModel(DataSet ds) { if (ds == null || ds…
DataTable 转实体对象 /// <summary> /// DataTable通过反射获取单个对象 /// </summary> public static T ToSingleModel<T>(this DataTable data) where T : new() { && data.Rows.Count < ) return data.GetList<T>(null, true).Single(); return defa…
using System; using System.Collections.Generic; using System.Data; using System.Reflection; namespace CommonSD { /// <summary> /// DataTable与实体类互相转换 /// </summary> /// <typeparam name="T">实体类</typeparam> public class Mode…
/// <summary> /// 用DataTable填充实体类List /// </summary> public static List<T> FillListModel<T>(DataTable dt) where T : new() { ) { return null; } List<T> modelList = new List<T>(); foreach (DataRow dr in dt.Rows) { //T mod…
2019.8.14 更新 补全了DataTable转泛型集合的方法: /// <summary> /// DataTable转实体类集合 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="dt">DataTable数据集</param> public static List<T> SetValue…
using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Reflection; using System.Text; namespace Common { public class ModelHandler<T> where T : new() { #region DataTable转换成实体类 /// <summary> /// 填充对象列…
前言,此方法利用反射将DataRow转成实体,由于反射性能不行,大家就看看就行了吧. 代码来啦 using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Reflection; using System.Text; namespace WangSql.DBUtility { public class DataMapHelper { private enum…
本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作. 文章是哥(mephisto)写的,SourceLink 阅读目录 介绍 起因 代码 UnitTest GitHub 本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作. 文章是哥(mephisto)写的,SourceLink 介绍 很多年前一直使用Ado.net,后来慢慢转型到其他的orm,在转型过程中,有意向将两者的模型结合起来,利用DataTable中的行…
public static T GetEntity<T>(DataTable table) where T : new()    {        T entity = new T();        foreach (DataRow row in table.Rows)        {            foreach (var item in entity.GetType().GetProperties())            {                if (row.T…
using System;using System.Collections.Generic;using System.Text;using System.Data;using System.Data.OracleClient;using System.Configuration;using System.Text.RegularExpressions;using DataAccess;using System.Diagnostics;using System.Reflection;namespa…
续上两篇文章,使用emit构造dynamic method,把 datareader转换为实体,以避免直接使用反射来实现带来的性能损失.代码看似没有纰漏,但是实际上我在framwork4下运行时,调用 dynamic method时, 系统都会报 “ 找不到方法 ‘?’  ”的错误,没有找到问题所在,网上查了下资料,发现在framwork3.5以上还可以用表达式树动态构造 执行的语句,并动态编译成方法.性能上与emit还是接近,,而且代码精简了许多. 废话不多说,上代码 public class…
using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Reflection; using System.Web; /// <summary> /// DbHelper 的摘要说明 /// </summary> public class DbHelper { public DbHelper() { // // TODO: 在此处添加构造函数逻…
介绍 介绍 很多年前一直使用Ado.net,后来慢慢转型到其他的orm,在转型过程中,有意向将两者的模型结合起来,利用DataTable中的行状态完善一些mvc中的数据控制作用.现在把它放出来,留个纪念. 起因 很多年前,对Ado.net这块了解较深,当时公司也有一套框架,将Ado.net做成了ORMapping,所以,当时对DataTable的操作很是熟练. DataTable中的行状态很好的和界面的数据后者操作进行了关联,比如新增,修改,取消,删除等,都能在DataTable中的行状态对应起…
机房收费系统大家想必不是做完.就是已经在手上了,在一開始做的时候就明白规定.我们必须用实体.而不能使Datatable,由于说是Datatable直接面向了数据库,当时不是非常明白,于是也没有再深究,就去实现代码部分了.如今最终把三层的机房收费系统做完了,回过头再看的时候,有了那么一点小认识. 去年的时候,师哥师姐们用的是Datatable.也都实现了三层.在网上查了一些小Dome.也都是datatable ,于是我们就会发现这样真的有意义吗. 那么我们先来看看Datatable和Dataset…
昨天找坤哥看到我的一段代码.例如以下: 略微解释下,这段代码时D层查询结束后,将datatable查询到的结果赋值给实体对象的属性,然后返回实体的过程.坤哥看了之后问我.假设实体有500多个属性.难道也要这样一条一条的写吗?假设返回多个实体时怎么办?这时.我才意识到自己的代码时非常有问题的,原来设计的是每一个方法最多返回一个实体,可是当遇到查询到多条记录的时候.就又冒着破坏三层结构的事返回Datatable去了.真的是非常有问题啊. 怎么改,我脑海中一下子就浮现了老办法:数组+循环,用循环读条读…
using System; using System.Collections.Generic; using System.Data; using System.Reflection; namespace Dll { public static class ToEntity { /// <summary> /// 将DataTable转换成实体类 /// </summary> /// <typeparam name="T">实体类</typepa…
public static class ModelConvertHelper<T> where T : class,new() { public static List<T> DataTableToModel(DataTable table) { List<T> ts = new List<T>(); foreach (DataRow dr in table.Rows) { T t = new T(); // 获得此模型的公共属性 PropertyInfo[…
public static T GetEntity<T>(DataTable table) where T : new() { T entity = new T(); foreach (DataRow row in table.Rows) { foreach (var item in entity.GetType().GetProperties()) { if (row.Table.Columns.Contains(item.Name)) { if (DBNull.Value != row[i…