SQLDataReader 行计数
我试图获取通过迭代读取器返回的行数。但是当我运行这段代码时我总是得到1?我是不是搞砸了什么?
int count = 0;
if (reader.HasRows)
{
while (reader.Read())
{
count++;
rep.DataSource = reader;
rep.DataBind();
}
}
resultsnolabel.Text += " " + String.Format("{0}", count) + " Results";
I am trying to get the number of rows that were returned by iterating the reader. But I always get 1 when I run this code? Did I screw up something in this?
int count = 0;
if (reader.HasRows)
{
while (reader.Read())
{
count++;
rep.DataSource = reader;
rep.DataBind();
}
}
resultsnolabel.Text += " " + String.Format("{0}", count) + " Results";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
SQLDataReaders 是只进的。你本质上是在这样做:
你可以这样做:
SQLDataReaders are forward-only. You're essentially doing this:
You could do this instead:
这将为您提供行数,但会将数据读取器留在最后。
This will get you the row count, but will leave the data reader at the end.
也许你可以尝试这个:不过请注意 - 这会拉动列数,而不是行数
Maybe you can try this: though please note - This pulls the column count, not the row count