惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Forbes - Security
Forbes - Security
T
Tailwind CSS Blog
Hugging Face - Blog
Hugging Face - Blog
Blog — PlanetScale
Blog — PlanetScale
WordPress大学
WordPress大学
aimingoo的专栏
aimingoo的专栏
Y
Y Combinator Blog
U
Unit 42
I
InfoQ
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
V
Visual Studio Blog
B
Blog RSS Feed
Vercel News
Vercel News
F
Fortinet All Blogs
Know Your Adversary
Know Your Adversary
T
Troy Hunt's Blog
博客园 - 【当耐特】
MongoDB | Blog
MongoDB | Blog
大猫的无限游戏
大猫的无限游戏
A
About on SuperTechFans
Jina AI
Jina AI
小众软件
小众软件
T
Threatpost
有赞技术团队
有赞技术团队
人人都是产品经理
人人都是产品经理
The Hacker News
The Hacker News
T
The Exploit Database - CXSecurity.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Microsoft Azure Blog
Microsoft Azure Blog
Recent Announcements
Recent Announcements
酷 壳 – CoolShell
酷 壳 – CoolShell
Scott Helme
Scott Helme
B
Blog
腾讯CDC
Last Week in AI
Last Week in AI
P
Proofpoint News Feed
S
Schneier on Security
N
News and Events Feed by Topic
Microsoft Security Blog
Microsoft Security Blog
K
Kaspersky official blog
G
Google Developers Blog
T
Tor Project blog
PCI Perspectives
PCI Perspectives
S
Secure Thoughts
Google Online Security Blog
Google Online Security Blog
Latest news
Latest news
Google DeepMind News
Google DeepMind News
MyScale Blog
MyScale Blog
罗磊的独立博客

博客园 - 农村的芬芳

PHP使用第三篇:生成数据库 使用THINKPHP产生的:No database selected [问题 从今天开始记录PHP使用的点点滴滴 留给自个看的工具提示 转xml Oracle 跨库 查询 复制表数据 关于海量数据处理 近日用到啦progressbar控件,将其用法留一下 select into 和 insert into select 两种表复制语句 采用regsvr32注册组件后提示:没有注册 net2005中将list<>数组转换为Table windows 服务操作 转载:XML与DataSet的相互转换类 oracle 日期函数小计 如何创建自定义帐户来运行 ASP.NET 通过ASP.net程序创建域帐户故障 EnterpriseLibrary服务问题 下载文件关闭窗体之解决方法 超级郁闷之问题,请DUDU及各位大位指正错误
将对象数组转换成dataset
农村的芬芳 · 2007-11-16 · via 博客园 - 农村的芬芳

在使用asp.net 2.0 beta 2 开发项目的过程中,碰到了将对象数组绑定到GridView的数据源上,但是使用排序的时候,却发现GridView并不提供对对象数组的支持,而在微软的官方网站上我们可以看到GridView的排序只有当数据源是DataSet的时候才支持,所以对于某些采用了O/R映射的项目或者返回的数据源是对象数组的项目中,就需要考虑提供将对象数组转换为DataSet的方法了,否则就要自己来手工来实现每一个GridView的排序,降低了开发效率。
  在微软的.net开发类库中,我们找不到现成转换方法,所以就需要考虑自行开发这个功能了,然而实现起来并不是很复杂,其中要用到的主要技术就是反射。利用反射,我们能够获取该对象数组中对象的每一个属性,包括其名称、类型,然后利用这些信息来创建DataSet中数据表的Schema,当创建完成以后,将该对象数组的每一个对象的属性数据复制到刚才创建的数据表中,这样就很简单的完成了该功能的实现,还是用代码来说的更清楚些:
文件:objectArrayToDataSet.cs

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Data;
    using System.Reflection;
    public class ArrayToDataSet
    {
        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 = new DataSet();
            DataTable dt = new DataTable();
            ds.Tables.Add(dt);

            PropertyInfo[] pis = t.GetProperties();
            foreach (PropertyInfo pi in pis)
            {
                DataColumn dc = new DataColumn(pi.Name, pi.PropertyType);
                dt.Columns.Add(dc);
            }
            return ds;
        }

        public static DataSet FillDataSet(DataSet ds, object[] objArr)
        {
            DataColumnCollection dcs = ds.Tables[0].Columns;
            Type t = objArr[0].GetType();
            foreach (object obj in objArr)
            {
                DataRow dr = ds.Tables[0].NewRow();
                for (int i = 0; i < dcs.Count; i++)
                {
                    dr[i] = t.InvokeMember(dcs[i].ColumnName, BindingFlags.GetProperty, null, obj, null);
                }
                ds.Tables[0].Rows.Add(dr);
            }
            return ds;
        }
    }