I have a datareader and I how can i know that, how many row this datareader have?

string query_ci = "SELECT [Kolon1],[Kolon2],[Kolon3] FROM[Sheet6$A:J] WHERE [Kolon1]='" + kolon_bul+ "'";
OleDbDataReader rdr_ci;
OleDbCommand komut_ci = new OleDbCommand(query_ci, conn);
rdr_ci = komut_ci.ExecuteReader();
     // ---> i want to know in here rows this "rdr_ci" have?
while (rdr_ci.Read())         
       // ---> or in here, i want to know in here how many times this "while" count in the first loop?
OleDbCommand komut = new OleDbCommand("UPDATE [Sheet6$] SET [Cat]='OK' WHERE [Sira]='" + rdr_ci["Sira"] + "'  ", baglanti);

Edited by fx5500: n/a

7 Years
Discussion Span
Last Post by alc6379

Since DataReader is a forward only reader, there is no way to know how many records are available from a Select statement. You'll have to count them as you read them. So initialize a counter before the while loop, and increment in the while loop. At the end you'll know how many records there were.


If you want to know the amount of rows that would be returned from a query, you could do a SELECT COUNT(0) FROM foo WHERE <conditions>. If you pass that query as the OleDbCommand, you could then do ExecuteScalar, and it would return the number of rows that your WHERE clause would return.

Other than that, Momerath's right-- you can't know ahead of time how many rows the datareader is going to return, unless the answer is 0. :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.