
Getting Record Count in SqlDataReader
When data is returned from SQL Server, it doesn't know ahead of time how
many records are going to be returned. In the case of a large result set,
records will start being returned before the entire query is complete - your
client will received the first record before the server know the total
record count. So the format of the on-the-wire protocol does not send a
"number of rows" count until *after* the very last record is sent.
The normal pattern for reading from a SqlDataReader is...
while (reader.Read()) { // is there another record?
// use reader to get the fields you want from this particular record
Quote:
}
--Don
--
This posting is provided "AS IS" with no warranties, and confers no rights.
Quote:
> Hi all, this sounds pretty trivial but I'd like to know
> how to obtain the record count in an SqlDataReader object
> used in a SELECT statement. The SqlDataReader class has a
> property called RecordsAffected which return an int data
> type; this property would have suffix but it returns -1
> when used with a SELECT statement.
> Using a DataSet object; ds; I could have something like
> this - ds.Tables[0].Rows.Count - return the record count.
> I'm not opting for a DataSet object 'cos it's very slow
> and we are looking at over 12,000 records at any
> successful SELECT query and over 8,000 possible queries at
> any time.
> I need to get the record count before running the Read()
> method in the SqlDataReader object or else the exception
> handler will be called if no record is found.
> Is there a way of getting the record count of an
> SqlDataReader object in a SELECT query before proceeding
> to the Read() while loop?
> Regards, Raphael.