C# 访问 DataReader 中的特定行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15450239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Access a specific row in DataReader
提问by HymanofAll
I have a datareader to display a list of gameweeks in a js carousel.
我有一个数据阅读器,可以在 js 轮播中显示游戏周列表。
I need to be able to add an if statement to change the div class of the current gameweek.
我需要能够添加一个 if 语句来更改当前游戏周的 div 类。
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?
实际上我想添加if(dreader[1])
然后添加额外的位,这怎么可能?
采纳答案by JamesWest
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();
回答by jiiri
I find it easier to use a DataTable or DataSet. Something like this:
我发现使用 DataTable 或 DataSet 更容易。像这样的东西:
DataTable dt = new DataTable();
dt.Load(dreader)
Then you can more easily reach a certain row using the DataTable.Rows property.
然后,您可以使用 DataTable.Rows 属性更轻松地到达某一行。
回答by Nikola Davidovic
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();
回答by Eddy Jawed
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;
要获取第一行,请从 0 开始并始终将其放入数据表中,因为直接从数据读取器读取总是会遇到问题;
if (dReader.HasRows) {
DataTable dt = new DataTable();
dt.Load(dreader)
gameweekList.Text = dt.Rows[0]["gameweekID"]
}