DataSet取值并保存在List集合中

DBHelper db = new DBHelper();   //实例化DB类
 
  string sql = "select * from student";    //虚构的sql语句 
 
   DataSet ds = db.getDataSet(sql);   //假设的DB类中已返回的数据 用DataSet接收
 
  List<student> students = new   List<student>();   //实例化一个集合 student对象为测试对象
 
 for(int i=0;i<ds.Table[0].rows.Count;i++)          //循环取出ds.table中的值
{
 
student s = new student();                  // 实例化student对象
 
 s.Id=Convert.ToInt32(ds.Table[0].row[i]["Id"]); 
 
 s.Name=Convert.ToInt32(ds.Table[0].row[i]["Id"]); 
 
 s.Age=Convert.ToInt32(ds.Table[0].row[i]["Id"]); 
 
 s.Sex=Convert.ToInt32(ds.Table[0].row[i]["Id"]); 
 
students.add(s);    // 将取出的对象保存在LIST中  以上是获得值。
 
 
}

ds.table[0].["Name"]     ds.table[0].["ID"]   ds.table[0].["Phone"]    括号内 表示对应的列名

原文地址:https://www.cnblogs.com/hanke123/p/5300586.html