How do I get an SqlDataReader to return the number of rows returned by a SELECT statement?

Posted on 2007-08-10
Last Modified: 2013-12-17
I use SqlDataReader and need to onkw how many rows were returned.  Is that possible?

Question by:newbieweb
    LVL 22

    Assisted Solution

    If you are loading a DataTable with the reader,
    int rowsReturned = myDataTable.Rows.Count;
    LVL 7

    Accepted Solution

    not without rendering your sqldatareader object useless.  By definition, the data reader has forward only access which means that you would have to scan all the records until you hit eof, which then would tell you how many records were there.  Better ways to do it are to either
    1. run a separate query with the 'count' keyword in it that returns the count of your records, or
    2. instead of using an sqldatareader use a datatable object and load them all in there.  note that this means all of your data will be in memory and it willh ave much more overhead than a straight sqldatareader

    good luck

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (…
    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now