Number of rows from OracleDataReader

Can someone tell me how to find the number of rows from OracleDataReader in .net 2.0?

+4
source share
2 answers

The OracleDataReader object represents read-only, read-only, in-memory result set. Unlike a DataSet , an OracleDataReader object remains connected and displays one row at a time.

Thus, he does not know how many lines will be. If you use adapter data, you can get the number of rows, as it retrieves the rows.

In your case, you need to get all the rows (if you only need to get the data) to get the number of rows:

 OracleDataReader reader = cmd.ExecuteReader(); int rowCount = 0; while (reader.Read()) { // your logic here rowCount++; } 

But if you do not need this data, it would be better to reformulate your stored procedure / request to explicitly indicate the number of rows.

+5
source
 OracleDataReader objReader = cmd.ExecuteReader(); while(cmdReader.Read()) nRegisters++; objReader = cmd.ExecuteReader(); 

You must reinitialize this objReader, because this "pointer" remains in the last position when you want to read it again ... and there is no way to return to the first cursor position.

if you need the register number that you get, you should implement COUNT (*) in select instead of counting each line, you should be practical with your code:

if you need citizens of a certain city:

 BEGIN OPEN REF_CUR FOR SELECT COUNT(*) AS nRegisters, City AS Var1, Country AS Var2 FROM Citizens WHERE City = 'City'; RETURN; END; 
+1
source

All Articles