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. Re-install Flyme or Native Google Android on Meizu MX4 Ubuntu (by quqi99)

    作者:张华 发表于:2017-06-23 版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明 ( http://blog.csdn.net/quqi99 ) ## ...

  2. not

    x = [] print(x) print(not x) print(x is None) print(not x is None) print(x is not None)

  3. netcat 瑞士军刀

    netcat被誉为网络安全界的‘瑞士军刀’,一个简单而有用的工具,透过使用TCP或UDP协议的网络连接去读写数据.它被设计成一个稳定的后门工具,能够直接由其它程序和脚本轻松驱动.同时,它也是一个功能强 ...

  4. vue前端开发那些事——vue开发遇到的问题

    vue web开发并不是孤立的.它需要众多插件的配合以及其它js框架的支持.本篇想把vue web开发的一些问题,拿出来讨论下.  1.web界面采用哪个UI框架?项目中引用了layui框架.引入框架 ...

  5. map/reduce类简单介绍

    在Hadoop的mapper类中,有4个主要的函数,分别是:setup,clearup,map,run.代码如下: protected void setup(Context context) thro ...

  6. 剑指Offer-第一章面试细节总结

    面试细节:行为面试(20%)+技术面试(70%)+应聘者提问(10%) * 行为面试:跳槽者(不要抱怨老板,不要抱怨同事,只为追寻自己的理想而站斗) * 技术面试:1.基础知识点(编程语言,数据结构( ...

  7. python SQLAlchemy自动生成models文件

    1.安装SQLAcodegen pip install sqlacodegen 2.执行 sqlacodegen mysql://root:123456@127.0.0.1:3306/test > ...

  8. spring mvc处理http请求报错:java.lang.IllegalStateException: getInputStream() has already been called for this request

    发送post请求到controller处理失败,报错日志如下: java.lang.IllegalStateException: getInputStream() has already been c ...

  9. python 数据库查询结果转对象

    #coding:utf-8 from json import dumps, loads, JSONEncoder, JSONDecoder import pickle from app.model.J ...

  10. 读取zip文件,不解压缩直接解析,支持文件名中文,解决内容乱码

    使用ant.jar进行文件zip压缩 import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import j ...