List轉(zhuǎn)換成DataSet實現(xiàn)代碼
更新時間:2012年12月13日 09:58:19 作者:
怎樣把List轉(zhuǎn)換成DataSet本人很是疑惑,于是搜集整理一番,需要的朋友可以參考下
復(fù)制代碼 代碼如下:
/// <summary>
/// List轉(zhuǎn)換成DataSet
/// </summary>
/// <typeparam name="T">類型</typeparam>
/// <param name="list">將要轉(zhuǎn)換的List</param>
/// <returns></returns>
public DataSet ConvertToDataSet<T>(IList<T> list)
{
if (list == null || list.Count <= 0)
{
return null;
}
DataSet ds = new DataSet();
DataTable dt = new DataTable(typeof(T).Name);
DataColumn column;
DataRow row;
System.Reflection.PropertyInfo[] myPropertyInfo = typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
foreach (T t in list)
{
if (t == null)
{
continue;
}
row = dt.NewRow();
for (int i = 0, j = myPropertyInfo.Length; i < j; i++)
{
System.Reflection.PropertyInfo pi = myPropertyInfo[i];
string name = pi.Name;
if (dt.Columns[name] == null)
{
column = new DataColumn(name, pi.PropertyType);
dt.Columns.Add(column);
}
row[name] = pi.GetValue(t, null);
}
dt.Rows.Add(row);
}
ds.Tables.Add(dt);
return ds;
}
C#使用OleDb操作Excel和數(shù)據(jù)庫的策略
在C#編程中,使用OleDb可以方便地實現(xiàn)對Excel文件和數(shù)據(jù)庫的操作,本文探討了在C#中使用OleDb技術(shù)操作Excel和數(shù)據(jù)庫的策略,文章詳述了OleDb的定義、配置環(huán)境的步驟,并通過實際代碼示例演示了如何高效讀寫Excel文件和交互數(shù)據(jù)庫,需要的朋友可以參考下
2024-05-05 
