首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SQLDataReader行数

SQLDataReader行数
EN

Stack Overflow用户
提问于 2011-04-01 00:07:33
回答 4查看 139K关注 0票数 22

我正在尝试通过迭代读取器来获取返回的行数。但是当我运行这段代码时,我总是得到1?是我搞砸了什么吗?

代码语言:javascript
复制
int count = 0;
if (reader.HasRows)
{
    while (reader.Read())
    {
        count++;
        rep.DataSource = reader;
        rep.DataBind();
    }
}
resultsnolabel.Text += " " + String.Format("{0}", count) + " Results";
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-04-01 00:14:33

SQLDataReaders是只向前的。您实际上是在这样做:

代码语言:javascript
复制
count++;  // initially 1
.DataBind(); //consuming all the records

//next iteration on
.Read()
//we've now come to end of resultset, thanks to the DataBind()
//count is still 1 

你可以这样做:

代码语言:javascript
复制
if (reader.HasRows)
{
    rep.DataSource = reader;
    rep.DataBind();
}
int count = rep.Items.Count; //somehow count the num rows/items `rep` has.
票数 32
EN

Stack Overflow用户

发布于 2013-01-31 23:59:55

代码语言:javascript
复制
 DataTable dt = new DataTable();
 dt.Load(reader);
 int numRows= dt.Rows.Count;
票数 22
EN

Stack Overflow用户

发布于 2014-03-06 06:34:38

这将使您获得行数,但会将数据读取器留在最后。

代码语言:javascript
复制
dataReader.Cast<object>().Count();
票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5502863

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档