From https://www.cnblogs.com/zjbky/p/9242140.html static class ExtendClass { public static List<T> ToDataList<T>(this DataTable dt) { var list = new List<T>(); var plist = new List<PropertyInfo>(typeof(T).GetProperties()); foreach…
用npoi方式,遇到一个问题,有的excel用加密软件(盖章,只读等)生成的解析不了,所以换成自带的方式,可以解决. 需要引用系统自带Microsoft.office.interop.excel public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string f = @"D:\bkintest.xlsx"; CreateExcelFile(…
http://clrprofiler.codeplex.com/ http://blogs.msdn.com/b/davbr/archive/2012/11/19/clrprofiler-4-5-released-includes-windows-store-app-support.aspx Rewrite MSIL Code on the Fly with the .NET Framework Profiling API Aleksandr Mikunov This article assum…
https://stackoverflow.com/questions/21628467/order-of-execution-with-multiple-filters-in-web-api Some things to note here: Filters get executed in the following order for an action: Globally Defined Filters -> Controller-specific Filters -> Action-s…
Having said that, here is a solution that you can use with the Unity container: Create some custom attributes for the different lifetime styles that you want to have like this: [AttributeUsage(AttributeTargets.Class)] public class SingletonAttribute…
public partial class ImportExcel : Form { AceessHelpers accessHelper = new AceessHelpers(); public ImportExcel() { InitializeComponent(); } private void btn_importExcelData(object sender, EventArgs e) { openFileDialog1.Title = "打开文件"; openFileDi…
public class TableHelper { public static DataTable CreateTableFromClass(Type t) { DataTable dt = new DataTable(); PropertyInfo[] pis = t.GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public); int colNum = t.Ge…
public static DataSet ObjectArrayToDataSet(object[] objArr) { if (objArr.Length == 0) return null; DataSet ds = CreateDataSet(objArr[0].GetType()); ds = FillDataSet(ds, objArr); return ds; } public static DataSet CreateDataSet(Type t) { DataSet ds =…
目录 . NTP简介 . NTP协议格式 . NTP Reflect反射漏洞和NTP协议的关系 . 漏洞触发的前提和攻击需要的步骤 . 针对漏洞的攻防思考 1. NTP简介 Network Time Protocol(NTP)是用来使计算机时间同步化的一种协议,它可以使计算机对其服务器或时钟源(如石英钟,GPS等等)做同步化,它可以提供高精准度的时间校正(LAN上与标准间差小于1毫秒,WAN上几十毫秒),且可介由加密确认的方式来防止恶毒的协议攻击. NTP提供准确时间,首先要有准确的时间来源,这…
在开发中,把查询结果以DataTable返回很方便,但是在检索数据时又很麻烦,没有list<T>检索方便.但是数据以ILIST形式返回,就为我们在.NET中使用传统的数据绑定造成了不便.下面介绍datatable和list的相互转换 1.datatable转换为list public List<T> GetList<T>(DataTable table) { List<T> list = new List<T>(); T t = default(…
引子 最近在项目中在数据库查询的时间,总是要用到数据表到实体类对象列表的转化,自己封装了一个转换的方法,用起来还比较方便,记下来,以后可以重复使用,原理就主要是利用反射,当然有更好的ORM框架可以实现,主要的原因就是我这里没有用orm. 实现 话不多少,直接上实现代码 /// <summary> /// 数据表转换类 /// </summary> /// <typeparam name="T"></typeparam> public cl…
Invoke : DataTableToList<City>.ConvertToModel(ds.Tables[0]).ToList<City>(); using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Reflection; using System.Web; namespace JsonDataInMVC.DBOperator { p…
一.前因和存在的问题 前面我写了一篇<使用Emit把Datatable转换为对象集合(List<T>)>的博文,其实起源于我自己编写的一个orm工具(见前面几篇博文有介绍),里面已有用emit把datareader转换为List<T>的实现方法,但是需要增加一个把DataTable转换为List<T>的方法,在网上搜索了一些代码,经过改造,加入缓存设计,整理了一下代码结构,简单测试没有问题后就发了<使用Emit把Datatable转换为对象集合(Lis…
public static List<T> ToList<T>(DataTable dt)        {            List<T> list = new List<T>();            if (dt == null) return list;            DataTableEntityBuilder<T> eblist = DataTableEntityBuilder<T>.CreateBuild…
在开发过程过程中有时候需要将List<T>泛型转换成DataTable.DataSet,可以利用反射机制将DataTable的字段与自定义类型的公开属性互相赋值. 1.List<T>泛型转换成DataTable 代码: /// <summary> /// List<T>转换成DataTable /// </summary> /// <typeparam name="T"></typeparam> ///…
using System; using System.Collections.Generic; using System.Data; using System.Reflection; /// <summary> /// 将DataTable数据源转换成实体类 /// </summary> /// <typeparam name="T">实体</typeparam> public static class ToModel<T>…
前言,此方法利用反射将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…
学习MVC的时候,使用List<T>来存储数据给前台,但是从数据库中直接读取得到的是DataTable,虽然可以直接循环DataTable来得到list,但是如果每个实体类都通过这样的得到的话,代码冗余.可以利用反射技术,写一个公共类来实现转化. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Collect…
  1.根据Dataset生成json格式的字符串,不管Dataset里面有多少个表都可以一一生成对应的json字符串,并一次性返回 private string dsToJson(DataSet ds) { System.Text.StringBuilder str = new System.Text.StringBuilder("["); ; o < ds.Tables.Count; o++) { str.Append("{"); str.Append(s…
using System;using System.Collections;using System.Collections.Generic;using System.Data;using System.Linq;using System.Text;using System.Web;using System.IO;using Newtonsoft.Json;using System.Reflection;using System.Runtime.InteropServices;using NPO…
/**********************************************************************************/ // 说明: 数据转换工具. // 注意: 对象列表转换为DataTable或DataTable转换为对象列表. // 字段参照由对象的PropertyName决定. // 数据模型类的属性名必需与字段名一致, 包括大小写一致. /*************************************************…
方法一:(拷贝直接可以使用,适合大批量资料, 上万笔) Microsoft.Office.Interop.Excel.Application appexcel = new Microsoft.Office.Interop.Excel.Application(); SaveFileDialog savefiledialog = new SaveFileDialog(); System.Reflection.Missing miss = System.Reflection.Missing.Value…
在考虑将表格数据导出成excel的时候,网上搜的时候找到一个特别合适的公共方法,可以将query查询出来的集合转换为datatable 需引用using System.Reflection; public static DataTable LINQToDataTable<T>(IEnumerable<T> varlist) { //定义要返回的DataTable对象 DataTable dtReturn = new DataTable(); // 保存列集合的属性信息数组 Prop…
在将DataTable转换为List<T>时,找到了网上的方案,原文链接:http://stackoverflow.com/questions/4593663/fetch-datarow-to-c-sharp-object. 使用时,遇到DbNull无法正常转换的问题,所以做了修正补充,继续发代码上来. 欢迎补充修正. using System; using System.Collections.Generic; using System.Linq; using System.Data; us…
ConvertJson.cs类 using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Reflection; using System.Collections; using System.Data.Common; namespace DotNet.Utilities { //JSON转换类 public class ConvertJson { #regi…
介绍:List/IEnumerable转换到DataTable/DataView,以及DataTable转换到List 正文: 一.List<T>/IEnumerable转换到DataTable/DataView 方法一: /// <summary> /// Convert a List{T} to a DataTable. /// </summary> private DataTable ToDataTable<T>(List<T> items…
利用反射避免了硬编码出现的错误,但是实体类的属性名必须和数据库名字对应(相同) 1.利用反射把DataTable的数据写到单个实体类 /// <summary> ///利用反射把DataTable的数据写到单个实体类 /// </summary> /// <typeparam name="T">实体类(model)</typeparam> /// <param name="dtSource">DataTab…
本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作. 文章是哥(mephisto)写的,SourceLink 阅读目录 介绍 起因 代码 UnitTest GitHub 本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作. 文章是哥(mephisto)写的,SourceLink 介绍 很多年前一直使用Ado.net,后来慢慢转型到其他的orm,在转型过程中,有意向将两者的模型结合起来,利用DataTable中的行…
这是数据库开发中经常遇到的问题,当然,这可以用现成的ORM框架来解决,但有些时候,如果DataSet/DataTable是第三方接口返回的,ORM就不方便了,还得自己处理. 反射自然必不可少的,另外考虑到DataTable中的ColumnName通常与Model的PropertyName并不严格对应,可以用Attribute来记录这种映射关系. 步骤1:先创建一个DataFieldAttribute类 using System; namespace Jimmy.ORM { [AttributeU…
public class List2DataTable     { public static string GetClassName(Type type) {             if (type == null) throw new ArgumentException("参数type不能为空"); if (type.HasAttribute<AliasAttribute>()) return type.GetAttribute<AliasAttribute&g…