摘要:List物件與DataSet操作函式分享
這兩天剛好有用到整理一些實用的方法分享給大家
| 方法名稱 | 說明 |
| GetDataSetByObjects | 依List物件反轉成DataSet |
| ObjectToDataSetSchema | 依物件產生DataSetSchema |
| DataSetXmlToDataSet | DataSetXml反轉成DataSet |
| DataSetXmlToObjects | DataSetXml反轉成List物件 |
List物件資料
[Serializable]
public class Member
{
public string Name
{
get;
set;
}
public string City
{
get;
set;
}
}
/// <summary>
/// 共用取得物件
/// </summary>
/// <returns></returns>
List<Member> GetList()
{
return new List<Member>()
{
new Member(){
City="Kaoshuing"
,Name="Kim"}
,new Member(){
City="Taipei"
,Name="Jia"}
};
}常用函式
/// <summary>
/// 依List物件反轉成DataSet
/// </summary>
/// <param name="objs"></param>
/// <returns></returns>
DataSet GetDataSetByObjects(object[] objs)
{
DataSet ds = new DataSet();
DataTable dt = new DataTable();
ds.Tables.Add(dt);
Type objType = objs[0].GetType();//取得物件的型別
PropertyInfo[] ps = objType.GetProperties();//依型別取得所有屬性資訊
//產生DataTable 的欄位
foreach (PropertyInfo p in ps)//循繞所有屬性
dt.Columns.Add(p.Name, p.PropertyType);
foreach (object obj in objs)//將資料塞到DataTable
{
DataRow row = dt.NewRow();
foreach (PropertyInfo p in ps)//循繞所有屬性
{
object o = p.GetValue(obj, null);//取得值
row[p.Name] = o;
}
dt.Rows.Add(row);
}
return ds;
}
/// <summary>
/// 依物件產生DataSetSchema
/// </summary>
/// <param name="obj">物件 例new Member()</param>
/// <param name="fullPathName">完整檔案路徑 例:c:\DataSet.xsd</param>
void ObjectToDataSetSchema(object obj, string fullFilePath)
{
Type objType = obj.GetType();
PropertyInfo[] ps = objType.GetProperties();
DataSet ds = new DataSet("DataSet");
DataTable dt = new DataTable(objType.Name);
ds.Tables.Add(dt);
foreach (PropertyInfo p in ps)
{
dt.Columns.Add(new DataColumn(p.Name, p.PropertyType));
}
ds.WriteXmlSchema(fullFilePath);
}
/// <summary>
/// DataSetXml反轉成DataSet
/// </summary>
/// <param name="xmlStr">xml字串</param>
/// <returns></returns>
DataSet DataSetXmlToDataSet(string xmlStr)
{
DataSet ds = new DataSet();
StringReader reader = new StringReader(xmlStr);
ds.ReadXml(reader);
return ds;
}
/// <summary>
/// DataSetXml反轉成List物件
/// </summary>
/// <param name="xmlStr">xml字串</param>
/// <param name="type">物件型別 例typeof(Member)</param>
/// <returns></returns>
object[] DataSetXmlToObjects(string xmlStr, Type type)
{
DataSet ds = new DataSet();
StringReader reader = new StringReader(xmlStr);
ds.ReadXml(reader);
ArrayList result = new ArrayList();
PropertyInfo[] ps = type.GetProperties();
foreach (DataRow row in ds.Tables[0].Rows)
{
object item = System.Activator.CreateInstance(type);//依型別建立物件
foreach (PropertyInfo p in ps)
{
p.SetValue(item, row[p.Name], null);
}
result.Add(item);
}
return (object[])result.ToArray(type);
}
/// 依List物件反轉成DataSet
/// </summary>
/// <param name="objs"></param>
/// <returns></returns>
DataSet GetDataSetByObjects(object[] objs)
{
DataSet ds = new DataSet();
DataTable dt = new DataTable();
ds.Tables.Add(dt);
Type objType = objs[0].GetType();//取得物件的型別
PropertyInfo[] ps = objType.GetProperties();//依型別取得所有屬性資訊
//產生DataTable 的欄位
foreach (PropertyInfo p in ps)//循繞所有屬性
dt.Columns.Add(p.Name, p.PropertyType);
foreach (object obj in objs)//將資料塞到DataTable
{
DataRow row = dt.NewRow();
foreach (PropertyInfo p in ps)//循繞所有屬性
{
object o = p.GetValue(obj, null);//取得值
row[p.Name] = o;
}
dt.Rows.Add(row);
}
return ds;
}
/// <summary>
/// 依物件產生DataSetSchema
/// </summary>
/// <param name="obj">物件 例new Member()</param>
/// <param name="fullPathName">完整檔案路徑 例:c:\DataSet.xsd</param>
void ObjectToDataSetSchema(object obj, string fullFilePath)
{
Type objType = obj.GetType();
PropertyInfo[] ps = objType.GetProperties();
DataSet ds = new DataSet("DataSet");
DataTable dt = new DataTable(objType.Name);
ds.Tables.Add(dt);
foreach (PropertyInfo p in ps)
{
dt.Columns.Add(new DataColumn(p.Name, p.PropertyType));
}
ds.WriteXmlSchema(fullFilePath);
}
/// <summary>
/// DataSetXml反轉成DataSet
/// </summary>
/// <param name="xmlStr">xml字串</param>
/// <returns></returns>
DataSet DataSetXmlToDataSet(string xmlStr)
{
DataSet ds = new DataSet();
StringReader reader = new StringReader(xmlStr);
ds.ReadXml(reader);
return ds;
}
/// <summary>
/// DataSetXml反轉成List物件
/// </summary>
/// <param name="xmlStr">xml字串</param>
/// <param name="type">物件型別 例typeof(Member)</param>
/// <returns></returns>
object[] DataSetXmlToObjects(string xmlStr, Type type)
{
DataSet ds = new DataSet();
StringReader reader = new StringReader(xmlStr);
ds.ReadXml(reader);
ArrayList result = new ArrayList();
PropertyInfo[] ps = type.GetProperties();
foreach (DataRow row in ds.Tables[0].Rows)
{
object item = System.Activator.CreateInstance(type);//依型別建立物件
foreach (PropertyInfo p in ps)
{
p.SetValue(item, row[p.Name], null);
}
result.Add(item);
}
return (object[])result.ToArray(type);
}