数据读取器不显示第一行 [英] Datareader not displaying first row

查看:28
本文介绍了数据读取器不显示第一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在遍历我的数据库以显示与球员相关联的联赛列表.如果玩家不是任何联盟的成员,则会显示一条消息告诉他们.

I am looping through my database to display a list of leagues a player is associated with. If a player is not a member of any leagues then a message displays to tell them.

这是代码

if (dReader.Read())
{          
    while (dReader.Read())
    {
        usersLeagues.Text += "<li class=\"li-myLeagues\"><a  href=\"leagueDetails.aspx?leagueID=" + (dReader["leagueID"].ToString()) + "\">" + (dReader["leagueName"].ToString()) + "</a></li>";
    }
}
else
{
    usersLeagues.Text = "You are currently not a part of any leagues";
}
dReader.Close();
conn.Close();

问题在于数据读取器未显示查询中的第一个联赛.

The issue is that the data reader is not displaying the first league in the query.

知道这是为什么吗?

推荐答案

更改

if (dReader.Read()){  

if (dReader.HasRows){

通过调用 if 语句中的 Read(),您实际上是在读取第一行数据.在 while 语句中再次调用 Read(),跳过第一个读取的行.

By calling the Read() in the if statement, you actually are reading the first row of data. Calling Read() again in the while statement, skips the first read row.

您可以使用 HasRows 属性来检查阅读器是否包含任何数据.

You can use HasRows property to check if the reader contains any data.

这篇关于数据读取器不显示第一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆