DataTable操作工具类DataTableHelper。
功能介绍:
- 将泛型集合填充为数据表
- 将泛型填充为数据表
- 将对象集合填充为数据表
- 将对象填充为数据表
- 将定IDictionary数据转换为DataSet数据
- 将数据表填充为泛型集合
- 将数据表填充为泛型集合
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Reflection; using System.Data; using System.Collections; namespace IM.Common { /// <summary> /// 数据操作 /// <para>创建作者:changjin</para> /// <para>创建日期:2011/07/26</para> /// </summary> public class DataHelper { /// <summary> /// 将泛型集合填充为数据表 /// <para>创建作者:changjin</para> /// <para>创建日期:2011/07/26</para> /// </summary> /// <param name="list">泛型集合</param> /// <param name="tableName">表名</param> /// <returns>数据表</returns> public static DataTable Fill<T>(IList<T> list, string tableName) { DataTable dt = new DataTable(tableName); T t = Activator.CreateInstance<T>(); Type type = t.GetType(); PropertyInfo[] properties = type.GetProperties().Where(p => p.MemberType == MemberTypes.Property && p.CanRead).ToArray(); foreach (PropertyInfo p in properties) { dt.Columns.Add(p.Name); } foreach (T t1 in list) { PropertyInfo[] properties1 = t1.GetType().GetProperties().Where(p => p.MemberType == MemberTypes.Property && p.CanRead).ToArray(); DataRow dr = dt.NewRow(); for (int i = 0; i < dt.Columns.Count; i++) { PropertyInfo propertyInfo = properties1.SingleOrDefault(p => p.Name == Convert.ToString(dt.Columns[i].ColumnName)); if (propertyInfo != null) { dr[dt.Columns[i].ColumnName] = propertyInfo.GetValue(t1, null); } } dt.Rows.Add(dr); } return dt; } /// <summary> /// 将泛型填充为数据表 /// <para>创建作者:changjin</para> /// <para>创建日期:2011/07/26</para> /// </summary> /// <param name="t">泛型</param> /// <param name="tableName">表名</param> /// <returns>数据表</returns> public static DataTable Fill<T>(T t, string tableName) { IList<T> list = new List<T>(); if (t != null) { list.Add(t); } return Fill<T>(list, tableName); } /// <summary> /// 将对象集合填充为数据表 /// <para>创建作者:changjin</para> /// <para>创建日期:2011/07/26</para> /// </summary> /// <param name="list">对象集合</param> /// <param name="tableName">表名</param> /// <returns>数据表</returns> public static DataTable Fill(IList list, string tableName) { DataTable dt = new DataTable(tableName); if (list.Count > 0) { PropertyInfo[] properties = list[0].GetType().GetProperties().Where(p => p.MemberType == MemberTypes.Property && p.CanRead).ToArray(); foreach (PropertyInfo p in properties) { dt.Columns.Add(p.Name); } foreach (var t in list) { PropertyInfo[] properties1 = t.GetType().GetProperties().Where(p => p.MemberType == MemberTypes.Property && p.CanRead).ToArray(); DataRow dr = dt.NewRow(); for (int i = 0; i < dt.Columns.Count; i++) { PropertyInfo propertyInfo = properties1.SingleOrDefault(p => p.Name == Convert.ToString(dt.Columns[i].ColumnName)); if (propertyInfo != null) { dr[dt.Columns[i].ColumnName] = propertyInfo.GetValue(t, null); } } dt.Rows.Add(dr); } } return dt; } /// <summary> /// 将对象填充为数据表 /// <para>创建作者:changjin</para> /// <para>创建日期:2011/07/26</para> /// </summary> /// <param name="obj">对象</param> /// <param name="tableName">表名</param> /// <returns>数据表</returns> public static DataTable Fill(Object obj, string tableName) { IList list = null; if (typeof(IEnumerable).IsAssignableFrom(obj.GetType())) { list = (IList)obj; } else { list = new ArrayList(); if (obj != null) { list.Add(obj); } } return Fill(list, tableName); } /// <summary> /// 将定IDictionary数据转换为DataSet数据 /// <para>创建作者:changjin</para> /// <para>创建日期:2011/07/26</para> /// </summary> /// <param name="dictionary">键值对数据:key表名,value实体(或实体集合)</param> /// <returns>DataSet数据集</returns> public static DataSet Fill(IDictionary dictionary) { DataSet ds = new DataSet(); foreach (DictionaryEntry de in dictionary) { DataTable dt = Fill(de.Value, de.Key.ToString()); ds.Tables.Add(dt); } return ds; } /// <summary> /// 将数据表填充为泛型集合 /// <para>创建作者:changjin</para> /// <para>创建日期:2011/07/26</para> /// </summary> /// <param name="dataTable">数据表</param> /// <returns>泛型集合</returns> public static IList<T> FillList<T>(DataTable dataTable) { IList<T> list = list = new List<T>(); if (dataTable != null && dataTable.Rows.Count > 0 && dataTable.Columns.Count > 0) { List<string> columnNameList = new List<string>(); for (int i = 0; i < dataTable.Columns.Count; i++) { columnNameList.Add(dataTable.Columns[i].ColumnName); } for (int i = 0; i < dataTable.Rows.Count; i++) { DataRow dr = dataTable.Rows[i]; T t = Activator.CreateInstance<T>(); Type type = t.GetType(); PropertyInfo[] properties = type.GetProperties().Where(p => p.MemberType == MemberTypes.Property && p.CanWrite).ToArray(); foreach (PropertyInfo p in properties) { if (columnNameList.Contains(p.Name) && dr[p.Name] != DBNull.Value) { if (p.PropertyType.IsGenericType) { p.SetValue(t, Convert.ChangeType(dr[p.Name], p.PropertyType.GetGenericArguments()[0]), null); } else { p.SetValue(t, Convert.ChangeType(dr[p.Name], p.PropertyType), null); } } } list.Add(t); } } return list; } /// <summary> /// 将数据表填充为泛型集合 /// <para>创建作者:changjin</para> /// <para>创建日期:2011/07/26</para> /// </summary> /// <param name="dataTable">数据表</param> /// <returns>泛型集合</returns> public static T Fill<T>(DataTable dataTable) { return FillList<T>(dataTable)[0]; } } }