第1个回答 2011-08-24
给你的StocktableSubLst 类写好构造函数,字段与你数据库查出的字段对应
例如你StocktableSubLst 类有两个字段string A 和 string B
构造函数为
public StocktableSubLst(string A, string B)
{
this.A=A;
this.B=B;
}
然后遍历赋值增加
List<StocktableSubLst> lt=null;
if(ds.tables.count>0)
{
lt=new List<StocktableSubLst>(ds.Tables[0].Rows.Count);
foreach(DataRow row in ds.Tables[0].Rows)
{
lt.Add(new StocktableSubLst(row["A"].ToString(),row["B"].ToString()));
}
}
第2个回答 2011-08-24
DataTable dt = ds.Tables[0];
List<StocktableSubLst> myList= DataTableToList<StocktableSubLst>(dt);
public static List<T> DataTableToList<T>(DataTable dt)
where T : new()
{
List<T> mylist = new List<T>();
if (dt != null && dt.Rows.Count != 0)
{
PropertyInfo[] pInfos = typeof(T).GetProperties();
foreach (DataRow dr in dt.Rows)
{
T obj = new T();
mylist.Add(obj);
foreach (PropertyInfo info in pInfos)
{
try
{
info.SetValue(obj, dr[info.Name], null);
}
catch
{
}
}
}
}
return mylist;
}
第3个回答 2011-08-24
需要遍历DataSet
List<T> lst=new List<T>();
if(ds.tables.count>0)
{
foreach(datarow row in ds.tables[0].rows)
{
lst.Add.......
}
}本回答被网友采纳