DataSet 互相转换 List
/// <summary>
/// List <-> DataSet
/// </summary>
public class IListDataSet
{
/// <summary>
/// 集合装换DataSet
/// </summary>
/// <param name="list">集合</param>
/// <returns></returns>
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>
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>
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>
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>
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);
}
}
DataSet 互相转换 List的更多相关文章
- DataSet、DataTable转换List(泛型集合与DataSet互相转换 )
using System.Data; using System.Reflection; using System.Collections; using System.Collections.Gener ...
- xml 与 DataSet 互相转换
本文转载:http://www.cnblogs.com/30ErLi/archive/2010/09/21/1832694.html XmlDatasetConvert 该类提供了四种方法: 1.将x ...
- DataTabel DataSet 对象 转换成json
public class DataTableConvertJson { #region dataTable转换成Json格式 /// <summary> ...
- c#实现list,dataset,DataTable转换成josn等各种转换方法总和
using System;using System.Collections.Generic;using System.Text;using System.Data;using System.Refle ...
- DataSet 反射转换成 List<T>
/// <summary> /// DataSet转换成指定返回类型的实体集合 /// </summary> /// <typeparam name="T&qu ...
- 使用linq对ado.net查询出来dataset集合转换成对象(查询出来的数据结构为一对多)
public async Task<IEnumerable<QuestionAllInfo>> GetAllQuestionByTypeIdAsync(int id) { st ...
- 把dataset对象转换成list集合方法
public static List<T> GetList<T>(DataTable table) where T:new() { List<T> list = n ...
- XML 与 DataSet/DataTable 互相转换实例(C#)——转载
// <summary> /// XML形式的字符串.XML文江转换成DataSet.DataTable格式 /// </summary> pub ...
- DataSet与Xml文件的互相转换
DataSet转换为xml文件 //将DataSet转换为xml文件 private static void ConvertDataSetToXMLFile(DataSet xmlD ...
随机推荐
- INotifyPropertyChanged接口的详细说明
在windows phone开发8.1:数据绑定中,我们了解了数据绑定的基本知识.今后几篇文章会继续深入了解数据绑定.今天我们来看在数据绑定中十分重要的INotifyPropertyChanged接口 ...
- 【C++竞赛 A】xxx的项链
时间限制:2s 内存限制:64MB 问题描述 xxx有一个长度为n的宝石链,宝石有m种不同的颜色.xxx想截取其中连续的一段做一个项链.为了让项链更漂亮,xxx希望项链中的宝石包含所有颜色. 输入描述 ...
- USB 3.0规范中译本 第2章 术语及缩略语
本文为CoryXie原创译文,转载及有任何问题请联系cory.xie#gmail.com. 本章列出并定义本规范通篇将使用的术语及缩略语. 术语/略缩语 定义 ACK(确认包) 表示积极肯定的握手包. ...
- HTML代码简写法:Emmet和Haml(转)
HTML代码写起来很费事,因为它的标签多. 一种解决方法是采用模板, 在别人写好的骨架内,填入自己的内容.还有一种就是我今天想要介绍的方法----简写法. 常用的简写法,目前主要是Emmet和Haml ...
- 【u006】海战
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 在峰会期间,武装部队得处于高度戒备.警察将监视每一条大街,军队将保卫建筑物,领空将布满了F-2003飞 ...
- 判断文件是否存在的另一种方法 _access 和 _waccess
函数原型: int _access( const char *path, int mode ); int _waccess( const wchar_t *path, int mode ); 示例代码 ...
- Java 之 assert (断言)
我们知道C/C++语言中有断言的功能(assert).在Java SE 1.4版本以后也增加了断言的特性. 断言是为了方便调试程序,并不是发布程序的组成部分.理解这一点是很关键的. 默认情况下,JVM ...
- [NPM] Run npm scripts with git hooks
In this lesson we will look about how we can integrate with git hooks to help enforce a certain leve ...
- android之ContentProvider和Uri具体解释
一.使用ContentProvider(内容提供者)共享数据 在android中ContentProvider的作用是对外共享数据,就是说能够通过ContentProvider把应用中的数据共享给其它 ...
- Erlang中频繁发送远程消息要注意的问题
http://avindev.iteye.com/blog/76373 注:这篇文章可能会有争议,欢迎提出意见 在Erlang中,如果要实现两个远程节点之间的通信,就需要通过网络来实现,对于消息发送, ...