2013-05-03 91 views
-3

如何获得一个DataReader的长度,是这样的:如何获取有多少元素数据读取器读取?

sqlDataReader dr = command.ExecuteReader(); 
dr.Read(); 

int L= dr.Length;// this doesn't work. 

+0

行数或列数? – 2013-05-03 14:27:55

+0

行亲爱的,在此先感谢 – Aan 2013-05-03 14:29:53

+0

http://stackoverflow.com/questions/1383315/how-to-get-number-of-rows-using-sqldatareader-in-c-sharp – Arshad 2013-05-03 14:30:49

回答

5

你可以简单地通过使用计数器跟踪你有多少项目已经已经DataReader读取。不过,我不相信有找出有多少行是不只是阅读它们中的任何通用方式:

int count = 0; 
while (dr.Read()) 
{ 
    // Use the row data, presumably 
    count++; 
} 
0

我不认为DataReader的提供本身就是一个Count属性。您需要循环数据读取器并增加一个变量,或者首先执行一个选择计数(*),其条件与您在命令执行中使用的条件相同。

0

有没有直接方法(至少我不知道)来计算行或列。在您的while循环中使用自定义计数器。

  • 通过读取所有行作为自定义操作来查找。喜欢;

while(dr.Read()) 
{ 
    // Do your operations.. 
    counter++; 
} 
  • 运行像Select Count(*)查询你的操作了。