C# 如何获取datareader已读取的元素数?

C# 如何获取datareader已读取的元素数?,c#,sql,database,winforms,sqldatareader,C#,Sql,Database,Winforms,Sqldatareader,如何获取数据读取器的长度,例如: sqlDataReader dr = command.ExecuteReader(); dr.Read(); int L= dr.Length;// this doesn't work. ?只需使用计数器,您就可以跟踪已从数据读取器读取的项目数量。然而,我不相信有任何通用的方法可以在不阅读它们的情况下确定有多少行: int count = 0; while (dr.Read()) { // Use the row data, presumably

如何获取数据读取器的长度,例如:

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

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

只需使用计数器,您就可以跟踪已从
数据读取器
读取的项目数量。然而,我不相信有任何通用的方法可以在不阅读它们的情况下确定有多少行:

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

我认为Datareader本身没有提供Count属性。您需要循环datareader并递增一个变量,还是先执行select count(*),条件与您在命令执行中使用的条件相同。

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

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

  • 在操作之前运行类似于
    Select Count(*)
    的查询

行数或列数?行数亲爱的,提前谢谢
while(dr.Read())
{
     // Do your operations..
     counter++;
}