Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Row count from OracleDataReader

Can any one tell me how to find row count from OracleDataReader in .net 2.0?

like image 622
Pradeep Avatar asked Oct 18 '10 13:10

Pradeep


People also ask

How do you count the number of rows in a dataset?

Use the COUNT aggregate function to count the number of rows in a table. This function takes the name of the column as its argument (e.g., id ) and returns the number of rows for this particular column in the table (e.g., 5).

What is counted by RowCount?

@@RowCount is a system variable which will have the value of last executed query's number of rows affected. It is used after any type of query such as 'insert', 'Update', 'Delete'. So, @@RowCount is used to check number of rows affected only after a query execution.

What does row count mean?

Row count is the number of available rows of seating in a vehicle.


2 Answers

An OracleDataReaderobject represents a forward-only, read-only, in-memory result set. Unlike the DataSet, the OracleDataReaderobject stays connected and fetches one row at a time.

So, it does not know how many rows there will be. If you use the data adapter, then you will be able to get a row count since it fetches the rows.

In your case you have to fetch all the rows (if you need to fetch the data only) to get the row count:

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

But if you don't need that data, it would be better to reformulate your stored procedure/query to return row count explicitly.

like image 188
Oleks Avatar answered Oct 01 '22 20:10

Oleks


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

You should re-initialize that objReader because that "pointer" stays in the last position when you want to read it again... and there's no an option to return to the first position of the cursor.

if you need the number of register what you get you should realize a COUNT(*) in the select instead of counting every row, you must be practical about your code:

like if you need the numbers of citizens of a specific city:

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

RETURN;
END;
like image 21
Diego Fernando Villarroel Diaz Avatar answered Oct 01 '22 22:10

Diego Fernando Villarroel Diaz