日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時間:2025/3/20 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 DataSet和List 泛型之间互相转换 (转载, 作者写的很好) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

DataSet和List<T> 泛型之間互相轉(zhuǎn)換 (轉(zhuǎn)載, 作者寫的很好)

//DataSet與泛型集合間的互相轉(zhuǎn)換

//利用反射機制將DataTable的字段與自定義類型的公開屬性互相賦值。

//注意:從DataSet到IList<T>的轉(zhuǎn)換,自定義類型的公開屬性必須與DataTable中的字段名稱

//一致,才能到達想要的結(jié)果。建議DataTable的定義從數(shù)據(jù)庫來,自定義類型用O/R Mapping的方式獲得。

//代碼說明

/// <summary>

/// 泛型集合與DataSet互相轉(zhuǎn)換

/// </summary>

using System.Data;

using System.Reflection;

using System.Collections;

using System.Collections.Generic;

using System;

namespace qdl
{

??? public class IListDataSet
??? {

??????? /// <summary>

??????? /// 集合裝換DataSet

??????? /// </summary>

??????? /// <param name="list">集合</param>

??????? /// <returns></returns>??

??????? /// 2008-8-1 22:08 HPDV2806

??????? public static DataSet ToDataSet(IList p_List)
??????? {

??????????? DataSet result = new DataSet();

??????????? DataTable _DataTable = new DataTable();

??????????? if (p_List.Count > 0)
??????????? {

??????????????? PropertyInfo[] propertys = p_List[0].GetType().GetProperties();

??????????????? foreach (PropertyInfo pi in propertys)
??????????????? {

??????????????????? _DataTable.Columns.Add(pi.Name, pi.PropertyType);

??????????????? }

??????????????? for (int i = 0; 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>

??????? /// 泛型集合轉(zhuǎn)換DataSet

??????? /// </summary>??

??????? /// <typeparam name="T"></typeparam>?

??????? /// <param name="list">泛型集合</param>?

??????? /// <returns></returns>??

??????? /// 2008-8-1 22:43 HPDV2806

??????? public static DataSet ToDataSet<T>(IList<T> list)
??????? {

??????????? return ToDataSet<T>(list, null);

??????? }

??????? /// <summary>

??????? /// 泛型集合轉(zhuǎn)換DataSet

??????? /// </summary>

??????? /// <typeparam name="T"></typeparam>

??????? /// <param name="p_List">泛型集合</param>

??????? /// <param name="p_PropertyName">待轉(zhuǎn)換屬性名數(shù)組</param>????

??????? /// <returns></returns>?????

??????? /// 2008-8-1 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 > 0)
??????????? {

??????????????? PropertyInfo[] propertys = p_List[0].GetType().GetProperties();

??????????????? foreach (PropertyInfo pi in propertys)
??????????????? {

??????????????????? if (propertyNameList.Count == 0)
??????????????????? {

??????????????????????? // 沒有指定屬性的情況下全部屬性都要轉(zhuǎn)換?

??????????????????????? _DataTable.Columns.Add(pi.Name, pi.PropertyType);

??????????????????? }

??????????????????? else
??????????????????? {

??????????????????????? if (propertyNameList.Contains(pi.Name))

??????????????????????????? _DataTable.Columns.Add(pi.Name, pi.PropertyType);

??????????????????? }

??????????????? }

??????????????? for (int i = 0; i < p_List.Count; i++)
??????????????? {

??????????????????? ArrayList tempList = new ArrayList();

??????????????????? foreach (PropertyInfo pi in propertys)
??????????????????? {

??????????????????????? if (propertyNameList.Count == 0)
??????????????????????? {

??????????????????????????? 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">待轉(zhuǎn)換數(shù)據(jù)表索引</param>???

??????? /// <returns></returns>????

??????? /// 2008-8-1 22:46 HPDV2806??

??????? public static IList<T> DataSetToIList<T>(DataSet p_DataSet, int p_TableIndex)
??????? {

??????????? if (p_DataSet == null || p_DataSet.Tables.Count < 0)

??????????????? return null;

??????????? if (p_TableIndex > p_DataSet.Tables.Count - 1)

??????????????? return null;

??????????? if (p_TableIndex < 0)

??????????????? p_TableIndex = 0;

??????????? DataTable p_Data = p_DataSet.Tables[p_TableIndex];

??????????? // 返回值初始化??????

??????????? IList<T> result = new List<T>();

??????????? for (int j = 0; 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 = 0; i < p_Data.Columns.Count; i++)
??????????????????? {

??????????????????????? // 屬性與字段名稱一致的進行賦值??????

??????????????????????? if (pi.Name.Equals(p_Data.Columns[i].ColumnName))
??????????????????????? {

??????????????????????????? // 數(shù)據(jù)庫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">待轉(zhuǎn)換數(shù)據(jù)表名稱</param>????

??????? /// <returns></returns>?????

??????? /// 2008-8-1 22:47 HPDV2806???

??????? public static IList<T> DataSetToIList<T>(DataSet p_DataSet, string p_TableName)
??????? {

??????????? int _TableIndex = 0;

??????????? if (p_DataSet == null || p_DataSet.Tables.Count < 0)

??????????????? return null;

??????????? if (string.IsNullOrEmpty(p_TableName))

??????????????? return null;

??????????? for (int i = 0; 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);

??????? }

??? }

??? /*****************

??? 使用范圍

??? 1 可以用在業(yè)務層中數(shù)據(jù)獲取,獲取DataSet的同時也可以轉(zhuǎn)為IList集合為調(diào)用者所使用。

??? 2 在WebServices中傳輸自定義類型使用,即傳遞參數(shù)都用DataSet類型(WebServices直接支持的數(shù)據(jù)類型),在使用前將其轉(zhuǎn)換為IList來使用。

??? * ******************************/

}

總結(jié)

以上是生活随笔為你收集整理的DataSet和List 泛型之间互相转换 (转载, 作者写的很好)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。