我正在尝试通过迭代读取器来获取返回的行数。但是当我运行这段代码时,我总是得到1?是我搞砸了什么吗?
int count = 0;
if (reader.HasRows)
{
while (reader.Read())
{
count++;
rep.DataSource = reader;
rep.DataBind();
}
}
resultsnolabel.Text += " " + String.Format("{0}", count) + " Results";发布于 2011-04-01 00:14:33
SQLDataReaders是只向前的。您实际上是在这样做:
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 你可以这样做:
if (reader.HasRows)
{
rep.DataSource = reader;
rep.DataBind();
}
int count = rep.Items.Count; //somehow count the num rows/items `rep` has.发布于 2013-01-31 23:59:55
DataTable dt = new DataTable();
dt.Load(reader);
int numRows= dt.Rows.Count;发布于 2014-03-06 06:34:38
这将使您获得行数,但会将数据读取器留在最后。
dataReader.Cast<object>().Count();https://stackoverflow.com/questions/5502863
复制相似问题