马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

DataSet取值并保存在List集合中

[复制链接]
查看20612 | 回复3 | 2013-1-13 17:38:40 | 显示全部楼层 |阅读模式
在ASP.NET中,从数据库中取值的方法有:

DataSet、DataReader

今天就讲解一下 如何用DataSet取值并且保存到List<>泛型集合中

举例:
DBHelper的类我们已经建好,直接调用即可


   
[C#] 纯文本查看 复制代码
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中  以上是获得值。


}



至此  你的LIST已经取到值。

本文中的student对象及sql语句为测试用。  




C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
飞/可爱朋 | 2014-5-8 00:18:25 | 显示全部楼层
nice
飞/可爱朋 | 2014-5-8 00:19:07 | 显示全部楼层
student 是不是一个model类
ibcadmin | 2014-5-8 00:23:32 | 显示全部楼层
飞/可爱朋 发表于 2014-5-7 16:19
student 是不是一个model类

是的
C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则