Access a specific row in DataReader

醉酒当歌 提交于 2019-12-23 10:49:35

问题


I have a datareader to display a list of gameweeks in a js carousel.

I need to be able to add an if statement to change the div class of the current gameweek.

This is my current code:

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();

In effect I want to add if(dreader[1]) then add the extra bit, how is this possible?


回答1:


How about...

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();



回答2:


I find it easier to use a DataTable or DataSet. Something like this:

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

Then you can more easily reach a certain row using the DataTable.Rows property.




回答3:


If I got you correctly, you can just count reader reads like this. Then when the count suits you, you can add something different:

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();

But if you want to have current and subsequent read at the same time you should firs read onceand then start reading in a loop like this:

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();



回答4:


To get the first row, start from 0 and always put it into a Data Tables because you always get problems reading directly from Data Readers;

if (dReader.HasRows) {

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

}


来源:https://stackoverflow.com/questions/15450239/access-a-specific-row-in-datareader

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!