读书人

数据库表里有7条记录却只显示出来1条

发布时间: 2012-01-06 22:55:18 作者: rapoo

数据库表里有7条记录,却只显示出来1条,程序错误在哪呢?求助
程序代码如下:
<%@page language= "C# " %>
<%@import namespace= "System.Data "%>
<%@import namespace= "System.Data.SqlClient "%>
<%@import namespace= "System.Data.OleDb "%>
<html>
<script language= "C# " runat=server>
void page_load(object serder,EventArgs e)
{
OleDbConnection oleconnection=new OleDbConnection( "provider=SQLOLEDB;database=chapter;user id=sa ");
OleDbCommand olecommand=new OleDbCommand();
OleDbDataReader oledatareader;
oleconnection.Open();
olecommand.Connection=oleconnection;
olecommand.CommandText= "select * from student ";
oledatareader=olecommand.ExecuteReader();
do
{
oledatareader.Read();
labcontext.Text+=oledatareader.GetString(0)+ " ";
labcontext.Text+=oledatareader.GetString(1)+ " ";
labcontext.Text+=oledatareader.GetInt32(2)+ " ";
labcontext.Text+=oledatareader.GetString(3)+ " "+ " <br> ";
}
while(oledatareader.NextResult());
olecommand=null;
oleconnection.Close();
oleconnection=null;
labcontext.Text+= "查找成功 ";
}
</script>
<body>
<form runat=server>
<asp:label id=labcontext runat=server /> <br>
</form>
</body>
</html>


已经在sql2000中建了一个名为chapter的数据库,里面有表student内容如下:
studentid name age address
97070701 kang 24 tianjin
97070702 li 21 shanghai
97070703 wang 20 beijing
97070704 zhao 23 beijing


97070705 liu 33 huangshan
97070706 ma 30 jinan
97070707 hu 27 qingdao
可是运行后结果是:
97070701 kang 24 tianjin
查找成功


只显示了第一条记录,没有显示其它六条,如果在数据库里删除第一条的话,那么就显示第二条.也就是说它只能显示一条.而这个程序是从一本书上得来的,目的是显示student表里的所有记录.请问是该程序哪有问题吗?谢谢大家!

[解决办法]
while(oledatareader.Read())
{

labcontext.Text+=oledatareader.GetString(0)+ " ";
labcontext.Text+=oledatareader.GetString(1)+ " ";
labcontext.Text+=oledatareader.GetInt32(2)+ " ";
labcontext.Text+=oledatareader.GetString(3)+ " "+ " <br> ";
}
[解决办法]

NextResult() 是取另一个结果集,不是下一行记录。


--------------------------------------------
析软网
http://www.parsesoft.net
致力于 DocBook 的应用,专注于开源软件的解析、手册/指南、以及应用文章、书籍的发布。

读书人网 >asp.net

热点推荐