5

我有一个数据阅读器,可以在 js 轮播中显示游戏周列表。

我需要能够添加一个 if 语句来更改当前游戏周的 div 类。

这是我当前的代码:

if (dReader.HasRows) {
    while (dReader.Read()) {                
        gameweekList.Text += "<div class=\"item\"><h4>Gameweek " + 
            (dReader["gameweekID"].ToString()) + "</h4></div>";
    }
} else {
    gameweekList.Text = "Error Finding Gameweeks";
}
dReader.Close();
conn.Close();

实际上我想添加if(dreader[1])然后添加额外的位,这怎么可能?

4

4 回答 4

7

我发现使用 DataTable 或 DataSet 更容易。像这样的东西:

DataTable dt = new DataTable();
dt.Load(dreader)

然后,您可以使用 DataTable.Rows 属性更轻松地到达某一行。

于 2013-03-16T14:22:11.537 回答
4

怎么样...

if (dReader.HasRows) {
    while (dReader.Read()) {

        if ( dReader["gameweekID"].ToString() == currentWeekId ) 
        {    
            gameweekList.Text += "<div class=\"somethingSpecial\"><h4>Gameweek " + 
            (dReader["gameweekID"].ToString()) + "</h4></div>";
        } 
        else 
        {
            gameweekList.Text += "<div class=\"item\"><h4>Gameweek " + 
            (dReader["gameweekID"].ToString()) + "</h4></div>";
        }
    }
} else {
    gameweekList.Text = "Error Finding Gameweeks";
}
dReader.Close();
conn.Close();
于 2013-03-23T17:16:15.773 回答
3

如果我理解正确,您可以像这样计算读者阅读量。然后当计数适合您时,您可以添加不同的内容:

int count = 0;
if (dReader.HasRows) {
    while (dReader.Read()) {
        if(count == 1)  //add special row              
            gameweekList.Text += "something special " + dReader["gameweekID"].ToString();          
        else
            gameweekList.Text += "<div class=\"item\"><h4>Gameweek " + 
            (dReader["gameweekID"].ToString()) + "</h4></div>";
        count++;
    }
} else {
    gameweekList.Text = "Error Finding Gameweeks";
}
dReader.Close();
conn.Close();

但是,如果您想同时读取当前和后续读取,您应该先读取一次,然后开始循环读取,如下所示:

if (dReader.HasRows) {
    string previousRead = string.Empty;
    dReader.Read();
    previousRead = dReader["gameweekID"].ToString();
    while (dReader.Read()) {
          //use current and previous read 
          //dReader["gameweekID"].ToString()
          //previousRead
          //update previousRead for the next read
          previousRead = dReader["gameweekID"].ToString();
    }
} else {
    gameweekList.Text = "Error Finding Gameweeks";
}
dReader.Close();
conn.Close();
于 2013-03-16T14:30:11.933 回答
1

要获取第一行,从 0 开始并始终将其放入数据表中,因为直接从数据读取器读取总是会遇到问题;

if (dReader.HasRows) {

DataTable dt = new DataTable();
dt.Load(dreader)
gameweekList.Text = dt.Rows[0]["gameweekID"]

}
于 2017-05-31T16:13:10.327 回答