Results 1 to 24 of 24

Thread: Data Reader row count

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    71

    Data Reader row count

    Other than using a counter within a loop, is there a method/function that will determine how many records were returned from a query that uses the DataReader. Somthing similar to the record count property for Active X ADO?

  2. #2
    Registered User jkw119's Avatar
    Join Date
    Oct 2001
    Location
    Pittsburgh
    Posts
    256
    are you using a dataset? because if you are, you can do the following...

    VB Code:
    1. EndofFile = somedataset.tables(0).rows.count()

  3. #3
    Lively Member
    Join Date
    Jan 2000
    Location
    treehouse
    Posts
    106
    No, it looks like he is using a data reader instead of a data set.

  4. #4
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Nope. When you execute a command which returns a DataReader, the database is using a mechanism known as a firehose cursor. You read one record at a time and once a record is read, that's it, no turning back. If you use stored procedures, you could return an output paramater that holds the record count.

  5. #5
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Yes its as Lethal said, the reader doesn't read ahead until the Read method is called so it doesn't know how many records there are until it reaches the end. You can append a count field into the query to find out.

    If your original query was this:
    "SELECT * FROM MyTable WHERE MyField=1"

    Then change it to this:
    "SELECT *,Count(*) AS RowCount FROM MyTable WHERE MyField=1"

    Now each datarow will have a RowCount field that has the total number of records that matched the query that you can check.

  6. #6
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612
    Edneeis, how would you do it if you were hitting a stored proecedure?

    Here is my select statement in my stored procedure.

    VB Code:
    1. Select Location, [Date], rootitem, ProductCode, SaleItems, SaleTotal From Item_Sales
    2.     Where [Date] >= @BeginDate And [Date] <= @EndDate And (ProductCode = '12' Or ProductCode = '13' Or ProductCode = '15' Or ProductCode = '17' Or ProductCode = '18' Or ProductCode = '19' Or ProductCode = '20' Or ProductCode = '61' Or ProductCode = '62' Or ProductCode = '63' Or ProductCode = '85' Or ProductCode = '86' Or ProductCode = '87' Or ProductCode = '88' Or ProductCode = '89' Or ProductCode = '90')
    David Wilhelm

  7. #7
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    VB Code:
    1. Select Location, [Date], rootitem, ProductCode, SaleItems, SaleTotal From Item_Sales, [color=red][b]Count(*) AS RowCount[/b][/color]
    2.     Where [Date] >= @BeginDate And [Date] <= @EndDate And (ProductCode = '12' Or ProductCode = '13' Or ProductCode = '15' Or ProductCode = '17' Or ProductCode = '18' Or ProductCode = '19' Or ProductCode = '20' Or ProductCode = '61' Or ProductCode = '62' Or ProductCode = '63' Or ProductCode = '85' Or ProductCode = '86' Or ProductCode = '87' Or ProductCode = '88' Or ProductCode = '89' Or ProductCode = '90')

  8. #8
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    Just an fyi
    If you try to read the count of the reader like this
    Code:
    count = reader["count"]
    it won't work

    the reader returns an object and you need to convert it to an int to get the value of the count
    Code:
    count = Convert.ToInt32(reader["count"])
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  9. #9
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612
    Edneeis, I tried that in my Store Procedure and I got the following error message.

    Error 156: Incorrect syntax near the keyword 'RowCount'

    I tried single and double quotes around RowCount and that didn't work either.
    David Wilhelm

  10. #10
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    post the code for your stored procedure.
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  11. #11
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612
    I did that already. Here it is again though.

    VB Code:
    1. Select Location, [Date], rootitem, ProductCode, SaleItems, SaleTotal From Item_Sales, Count(*) AS RowCount
    2.     Where [Date] >= @BeginDate And [Date] <= @EndDate And (ProductCode = '12' Or ProductCode = '13' Or ProductCode = '15' Or ProductCode = '17' Or ProductCode = '18' Or ProductCode = '19' Or ProductCode = '20' Or ProductCode = '61' Or ProductCode = '62' Or ProductCode = '63' Or ProductCode = '85' Or ProductCode = '86' Or ProductCode = '87' Or ProductCode = '88' Or ProductCode = '89' Or ProductCode = '90')
    David Wilhelm

  12. #12
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    You had the RowCount after the From statement...change it to this
    Code:
    Select Location, [Date], rootitem, ProductCode, SaleItems, SaleTotal, Count(*) AS RowCount From Item_Sales 
    	Where [Date] >= @BeginDate And [Date] <= @EndDate And (ProductCode = '12' Or ProductCode = '13' Or ProductCode = '15' Or ProductCode = '17' Or ProductCode = '18' Or ProductCode = '19' Or ProductCode = '20' Or ProductCode = '61' Or ProductCode = '62' Or ProductCode = '63' Or ProductCode = '85' Or ProductCode = '86' Or ProductCode = '87' Or ProductCode = '88' Or ProductCode = '89' Or ProductCode = '90')
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  13. #13
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    That was my fault I cut and pasted quickly and stuck the RowCount in the wrong spot. I though the second line started with the FROM but it starts with the WHERE. Sorry about that.

  14. #14
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612
    This still doesn't work..

    VB Code:
    1. CREATE PROC dbo.CigUnits
    2.     @BeginDate Datetime, @EndDate datetime
    3.  
    4. as
    5. If @BeginDate is null or @EndDate is null
    6. Begin
    7.     Raiserror('Null values are not allowed',14,1)
    8.     Return
    9. End
    10.     Select Location, [Date], rootitem, ProductCode, SaleItems, SaleTotal, Count(*) as RowCount From Item_Sales
    11.     Where [Date] >= @BeginDate And [Date] <= @EndDate And (ProductCode = '12' Or ProductCode = '13' Or ProductCode = '15' Or ProductCode = '17' Or ProductCode = '18' Or ProductCode = '19' Or ProductCode = '20' Or ProductCode = '61' Or ProductCode = '62' Or ProductCode = '63' Or ProductCode = '85' Or ProductCode = '86' Or ProductCode = '87' Or ProductCode = '88' Or ProductCode = '89' Or ProductCode = '90')
    12. GO

    I get error message.
    Err 156: Incorrect syntax near the keyword 'RowCount'

    I also tried to use Exp1 and I tried putint single quotes around RowCount
    David Wilhelm

  15. #15
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    if you're using Sql Server you need to put tick (') marks around the RowCount variable.
    Code:
    Select Location, [Date], rootitem, ProductCode, SaleItems, SaleTotal, Count(*) AS 'RowCount' From Item_Sales
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  16. #16
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612
    Dude, I just said I put single quotes around RowCount which causes the error attached to this message.
    Attached Images Attached Images  
    David Wilhelm

  17. #17
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Yeah I guess I was wrong because I just tried it and got an error. I could have swore i've used it before though. Maybe it only works in GroupBy queries. Anyway since you are running a stored procedure you can still get it, give me a sec.

  18. #18
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    Because you're using an aggregate function..you need to use group by in you sql statement...so try using something like
    Code:
    GROUP BY Location
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  19. #19
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Yes it should work if it is grouped or you can just run a seperate count query in the stored procedure here is an example:

    ALTER PROCEDURE getSamplewCount
    @ID as int
    AS
    DECLARE @Num As Int
    SELECT @Num=(SELECT Count(*) FROM Sample WHERE Sample.ID=@ID)


    SELECT *, @Num As 'RowCount'
    FROM Sample
    WHERE (Sample.ID=@ID)

    RETURN

  20. #20
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612
    Where would the Group By code go?

    VB Code:
    1. CREATE PROC dbo.CigUnits
    2.     @BeginDate Datetime, @EndDate datetime
    3.  
    4. as
    5. If @BeginDate is null or @EndDate is null
    6. Begin
    7.     Raiserror('Null values are not allowed',14,1)
    8.     Return
    9. End
    10.     Select Location, [Date], rootitem, ProductCode, SaleItems, SaleTotal, Count(*) As RowCount From Item_Sales
    11.     Where [Date] >= @BeginDate And [Date] <= @EndDate And (ProductCode = '12' Or ProductCode = '13' Or ProductCode = '15' Or ProductCode = '17' Or ProductCode = '18' Or ProductCode = '19' Or ProductCode = '20' Or ProductCode = '61' Or ProductCode = '62' Or ProductCode = '63' Or ProductCode = '85' Or ProductCode = '86' Or ProductCode = '87' Or ProductCode = '88' Or ProductCode = '89' Or ProductCode = '90')
    12. GO
    David Wilhelm

  21. #21
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    at the very end of you procedure
    Code:
    Select Location, [Date], rootitem, ProductCode, SaleItems, SaleTotal, Count(*) As RowCount 
    From Item_Sales 
    Where [Date] >= @BeginDate And [Date] <= @EndDate And (ProductCode = '12' Or ProductCode = '13' Or ProductCode = '15' Or ProductCode = '17' Or ProductCode = '18' Or ProductCode = '19' Or ProductCode = '20' Or ProductCode = '61' Or ProductCode = '62' Or ProductCode = '63' Or ProductCode = '85' Or ProductCode = '86' Or ProductCode = '87' Or ProductCode = '88' Or ProductCode = '89' Or ProductCode = '90')
    GROUP BY Location
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  22. #22
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612
    Still doesn't work.

    VB Code:
    1. CREATE PROC dbo.CigUnits
    2.     @BeginDate Datetime, @EndDate datetime
    3.  
    4. as
    5. If @BeginDate is null or @EndDate is null
    6. Begin
    7.     Raiserror('Null values are not allowed',14,1)
    8.     Return
    9. End
    10.     Select Location, [Date], rootitem, ProductCode, SaleItems, SaleTotal, Count(*) as RowCount
    11.     From Item_Sales
    12.     Where [Date] >= @BeginDate And [Date] <= @EndDate And (ProductCode = '12' Or ProductCode = '13' Or ProductCode = '15' Or ProductCode = '17' Or ProductCode = '18' Or ProductCode = '19' Or ProductCode = '20' Or ProductCode = '61' Or ProductCode = '62' Or ProductCode = '63' Or ProductCode = '85' Or ProductCode = '86' Or ProductCode = '87' Or ProductCode = '88' Or ProductCode = '89' Or ProductCode = '90')
    13. Group By Location
    14. GO

    Same error message. Incorrect syntax near RowCount.

    I tried the Group by before and after the GO command.

    I tried single and double quotes, and nothing around RowCount.

    Guess I'll just do an additional sql call to get the record count.
    David Wilhelm

  23. #23
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    Sorry for bumping in, but when you want to count rows, the easiest thing to do is to use a select count(whatever) and then use the executescalar? Then you don't have to iterate through the entire reader to get the result... easier than the SP too...

    just my two cents
    Henrik

  24. #24
    New Member
    Join Date
    Aug 2015
    Posts
    1

    Re: Data Reader row count

    You just needed brackets [] around your rowcount statement [rowcount]

    -D-

    Quote Originally Posted by indydavid32 View Post
    Still doesn't work.

    VB Code:
    1. CREATE PROC dbo.CigUnits
    2.     @BeginDate Datetime, @EndDate datetime
    3.  
    4. as
    5. If @BeginDate is null or @EndDate is null
    6. Begin
    7.     Raiserror('Null values are not allowed',14,1)
    8.     Return
    9. End
    10.     Select Location, [Date], rootitem, ProductCode, SaleItems, SaleTotal, Count(*) as RowCount
    11.     From Item_Sales
    12.     Where [Date] >= @BeginDate And [Date] <= @EndDate And (ProductCode = '12' Or ProductCode = '13' Or ProductCode = '15' Or ProductCode = '17' Or ProductCode = '18' Or ProductCode = '19' Or ProductCode = '20' Or ProductCode = '61' Or ProductCode = '62' Or ProductCode = '63' Or ProductCode = '85' Or ProductCode = '86' Or ProductCode = '87' Or ProductCode = '88' Or ProductCode = '89' Or ProductCode = '90')
    13. Group By Location
    14. GO

    Same error message. Incorrect syntax near RowCount.

    I tried the Group by before and after the GO command.

    I tried single and double quotes, and nothing around RowCount.

    Guess I'll just do an additional sql call to get the record count.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width