using System.Data;

 using System.Reflection;

 using System.Collections;

 using System.Collections.Generic;

 using System;

 namespace BI.ERP.DAL
{
public class DataSet转换List
{
/// <summary> /// 泛型集合与DataSet互相转换 /// </summary> public class IListDataSet
{
/// <summary> /// 集合装换DataSet /// </summary> /// <param name="list">集合</param> /// <returns></returns> /// 2008-08-01 22:08 HPDV2806 public static DataSet ToDataSet(IList p_List)
{ DataSet result = new DataSet(); DataTable _DataTable = new DataTable(); if (p_List.Count > )
{ PropertyInfo[] propertys = p_List[].GetType().GetProperties(); foreach (PropertyInfo pi in propertys)
{ _DataTable.Columns.Add(pi.Name, pi.PropertyType); } for (int i = ; i < p_List.Count; i++)
{ ArrayList tempList = new ArrayList(); foreach (PropertyInfo pi in propertys)
{ object obj = pi.GetValue(p_List[i], null); tempList.Add(obj); } object[] array = tempList.ToArray(); _DataTable.LoadDataRow(array, true); } } result.Tables.Add(_DataTable); return result; } /// <summary> /// 泛型集合转换DataSet /// </summary> /// <typeparam name="T"></typeparam> /// <param name="list">泛型集合</param> /// <returns></returns> /// 2008-08-01 22:43 HPDV2806 public static DataSet ToDataSet<T>(IList<T> list)
{ return ToDataSet<T>(list, null); } /// <summary> /// 泛型集合转换DataSet /// </summary> /// <typeparam name="T"></typeparam> /// <param name="p_List">泛型集合</param> /// <param name="p_PropertyName">待转换属性名数组</param> /// <returns></returns> /// 2008-08-01 22:44 HPDV2806 public static DataSet ToDataSet<T>(IList<T> p_List, params string[] p_PropertyName)
{ List<string> propertyNameList = new List<string>(); if (p_PropertyName != null) propertyNameList.AddRange(p_PropertyName); DataSet result = new DataSet(); DataTable _DataTable = new DataTable(); if (p_List.Count > )
{ PropertyInfo[] propertys = p_List[].GetType().GetProperties(); foreach (PropertyInfo pi in propertys)
{ if (propertyNameList.Count == )
{ // 没有指定属性的情况下全部属性都要转换 _DataTable.Columns.Add(pi.Name, pi.PropertyType); } else
{ if (propertyNameList.Contains(pi.Name)) _DataTable.Columns.Add(pi.Name, pi.PropertyType); } } for (int i = ; i < p_List.Count; i++)
{ ArrayList tempList = new ArrayList(); foreach (PropertyInfo pi in propertys)
{ if (propertyNameList.Count == )
{ object obj = pi.GetValue(p_List[i], null); tempList.Add(obj); } else
{ if (propertyNameList.Contains(pi.Name))
{ object obj = pi.GetValue(p_List[i], null); tempList.Add(obj); } } } object[] array = tempList.ToArray(); _DataTable.LoadDataRow(array, true); } } result.Tables.Add(_DataTable); return result; } /// <summary> /// DataSet装换为泛型集合 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="p_DataSet">DataSet</param> /// <param name="p_TableIndex">待转换数据表索引</param> /// <returns></returns> /// 2008-08-01 22:46 HPDV2806 public static IList<T> DataSetToIList<T>(DataSet p_DataSet, int p_TableIndex)
{ if (p_DataSet == null || p_DataSet.Tables.Count < ) return null; if (p_TableIndex > p_DataSet.Tables.Count - ) return null; if (p_TableIndex < ) p_TableIndex = ; DataTable p_Data = p_DataSet.Tables[p_TableIndex]; // 返回值初始化 IList<T> result = new List<T>(); for (int j = ; j < p_Data.Rows.Count; j++)
{ T _t = (T)Activator.CreateInstance(typeof(T)); PropertyInfo[] propertys = _t.GetType().GetProperties(); foreach (PropertyInfo pi in propertys)
{ for (int i = ; i < p_Data.Columns.Count; i++)
{ // 属性与字段名称一致的进行赋值 if (pi.Name.Equals(p_Data.Columns[i].ColumnName))
{ // 数据库NULL值单独处理 if (p_Data.Rows[j][i] != DBNull.Value) pi.SetValue(_t, p_Data.Rows[j][i], null); else pi.SetValue(_t, null, null); break; } } } result.Add(_t); } return result; } /// <summary> /// DataSet装换为泛型集合 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="p_DataSet">DataSet</param> /// <param name="p_TableName">待转换数据表名称</param> /// <returns></returns> /// 2008-08-01 22:47 HPDV2806 public static IList<T> DataSetToIList<T>(DataSet p_DataSet, string p_TableName)
{ int _TableIndex = ; if (p_DataSet == null || p_DataSet.Tables.Count < ) return null; if (string.IsNullOrEmpty(p_TableName)) return null; for (int i = ; i < p_DataSet.Tables.Count; i++)
{ // 获取Table名称在Tables集合中的索引值 if (p_DataSet.Tables[i].TableName.Equals(p_TableName))
{ _TableIndex = i; break; } } return DataSetToIList<T>(p_DataSet, _TableIndex); } // <summary> /// DataTable装换为泛型集合 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="p_DataTable">p_DataTable</param> /// <returns></returns> /// 2008-08-01 22:47 HPDV2806
public static IList<T> DataTableToIList<T>(DataTable p_DataTable)
{ if (p_DataTable == null || p_DataTable.Rows.Count < ) return null; //DataTable p_Data = p_DataSet.Tables[p_TableIndex]; // 返回值初始化 IList<T> result = new List<T>(); for (int j = ; j < p_DataTable.Rows.Count; j++)
{ T _t = (T)Activator.CreateInstance(typeof(T)); PropertyInfo[] propertys = _t.GetType().GetProperties(); foreach (PropertyInfo pi in propertys)
{ for (int i = ; i < p_DataTable.Columns.Count; i++)
{ // 属性与字段名称一致的进行赋值 if (pi.Name.Equals(p_DataTable.Columns[i].ColumnName))
{ // 数据库NULL值单独处理 if (p_DataTable.Rows[j][i] != DBNull.Value) pi.SetValue(_t, p_DataTable.Rows[j][i], null); else pi.SetValue(_t, null, null); break; } } } result.Add(_t); } return result; } }
}
}

调用方法

  #region 

          List<Customer_model> CMList = DataSet转换List.IListDataSet.DataTableToIList<Customer_model>(da) as List<Customer_model>;

  #endregion

DataSet、DataTable转换List(泛型集合与DataSet互相转换 )的更多相关文章

  1. 泛型集合与DataSet相互转换

    一.泛型转DataSet /// <summary> /// 泛型集合转换DataSet /// </summary> /// <typeparam name=" ...

  2. C# DataSet装换为泛型集合

    1.DataSet装换为泛型集合(注意T实体的属性其字段类型与dataset字段类型一一对应) #region DataSet装换为泛型集合 /// <summary> /// 利用反射和 ...

  3. 泛型学习第三天——C#读取数据库返回泛型集合 把DataSet类型转换为List<T>泛型集合

    定义一个类: public class UserInfo    {        public System.Guid ID { get; set; } public string LoginName ...

  4. C#;DataTable添加列;DataTable转List泛型集合;List泛型集合转DataTable泛型集合;

    给DataTable添加列 string sql = "select * from cgpmb order by code"; DataTable dt = Bobole.Data ...

  5. DataSet装换为泛型集合 222

    #region DataSet装换为泛型集合 /// <summary> /// 利用反射和泛型 /// </summary> /// <param name=" ...

  6. DataSet和List<T> 泛型之间互相转换 (转载, 作者写的很好)

    /DataSet与泛型集合间的互相转换 //利用反射机制将DataTable的字段与自定义类型的公开属性互相赋值. //注意:从DataSet到IList<T>的转换,自定义类型的公开属性 ...

  7. C#中DataTable与泛型集合互转(支持泛型集合中对象包含枚举)

    最近在做WCF,因为是内部接口,很多地方直接用的弱类型返回(DataSet),这其实是一种非常不好的方式,最近将项目做了修改,将所有接口返回值都修改成强类型,这样可以减少很多与客户端开发人员的沟通,结 ...

  8. DataTable填补了实体类返回泛型集合

    坤哥见我昨天找了一段代码,如下面: 略微解释下,这段代码时D层查询结束后,将datatable查询到的结果赋值给实体对象的属性,然后返回实体的过程.坤哥看了之后问我,假设实体有500多个属性,难道也要 ...

  9. 使用泛型集合取代datatable作为返回值实现面向对象

    开会的时候,师父说.我们在机房重构时,尽量不要用datatable作为返回值.改用泛型集合的方式,这样能够实现真正的面向对象. 通过查资料和同学交流,把这个问题给攻克了. 对于泛型集合.我也有了一些认 ...

随机推荐

  1. PhotoPicker 从头到脚

    1. 简介 PhotoPicker, 是一款开源的图片选择器.效果上和微信相似. 2. 使用方法 2.1 添加依赖 dependencies { compile 'me.iwf.photopicker ...

  2. Markdown list状态下插入代码

    /***************************************************************************** * Markdown list状态下插入代 ...

  3. Java进阶知识点8:高可扩展架构的利器 - 动态模块加载核心技术(ClassLoader、反射、依赖隔离)

    一.背景 功能模块化是实现系统能力高可扩展性的常见思路.而模块化又可分为静态模块化和动态模块化两类: 1. 静态模块化:指在编译期可以通过引入新的模块扩展系统能力.比如:通过maven/gradle引 ...

  4. 深入了解zookeeper(三)

    一.ZooKeeper 的实现 1.1 ZooKeeper处理单点故障 我们知道可以通过ZooKeeper对分布式系统进行Master选举,来解决分布式系统的单点故障,如图所示. 那么我们继续分析一下 ...

  5. js错误Cannot set property 'action' of null

    Cannot set property 'action' of null [自己解决问题答案] 应该放到form里面 [网上答案]是页面无法加载完毕执行代码.可以把获取元素等一系列的操作放在 wind ...

  6. MySQL免安装版安装配置、修改密码

    一:MySQL的下载安装 1.1 下载 我下载的是 ZIP Archive 版的,win7 64位的机器支持使用,而且相对而言,简单.干净. 首先,进入MySQL的官方网址,依次点击Downloads ...

  7. Linux常用命令(个人使用频率较高)

    1,日志查看 tail(cat) -f|grep ERROR(任意字符) filepath (任意行数) -f|grep ERROR(任意字符) filepath 2,查看目录&授权 file ...

  8. Linux Change The I/O Scheduler For A Hard Disk

    ow do I change the I/O scheduler for a particular hard disk without rebooting my Linux server system ...

  9. linux字符界面下root用户无法登录成功

    os: rhel5.6_x86_64 ———————————————————————————————————————————— 故障:图形界面登录正常,其他一切正常,但是切换到字符界面时,输入用户ro ...

  10. unittest之跳过用例(skip) (含如何调用类里面函数相互调取变量的方法)

    当测试用例写完后,有些模块有改动时候,会影响到部分用例的执行,这个时候我们希望暂时跳过这些用例. 或者前面某个功能运行失败了,后面的几个用例是依赖于这个功能的用例,如果第一步就失败了,后面的用例也就没 ...