Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Getting the number of records in a recordset

Getting the number of records in a recordset

Scheduled Pinned Locked Moved C / C++ / MFC
databasequestion
14 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SWDevil
    wrote on last edited by
    #1

    hi, I have a recordset which contains the results of a query, and I want to get the number of records in the recordset. I tried to use the GetRecordCount function, but I understood that I have to go through the whole recordset in order to get the number of records. So I have this loop in my code which is very inefficient: while (!pSet->IsEOF()) //pSet is a pointer to the recordset pSet->MoveNext(); I thouhgt to do an SQL query using COUNT, but it requires a simple query (something like: SELECT COUNT (*) FROM Table1 ) and my query is not a simple one. It contains selection of a number of records, and Inner joins of tables and an ORDER BY clause. Is there another way to get the number of records in a recordset? Any suggestions?

    M D 2 Replies Last reply
    0
    • S SWDevil

      hi, I have a recordset which contains the results of a query, and I want to get the number of records in the recordset. I tried to use the GetRecordCount function, but I understood that I have to go through the whole recordset in order to get the number of records. So I have this loop in my code which is very inefficient: while (!pSet->IsEOF()) //pSet is a pointer to the recordset pSet->MoveNext(); I thouhgt to do an SQL query using COUNT, but it requires a simple query (something like: SELECT COUNT (*) FROM Table1 ) and my query is not a simple one. It contains selection of a number of records, and Inner joins of tables and an ORDER BY clause. Is there another way to get the number of records in a recordset? Any suggestions?

      M Offline
      M Offline
      Monty2
      wrote on last edited by
      #2

      Been a long time since i programmed in ADO but IIRC you don't need to

      while (!pSet->IsEOF()) //pSet is a pointer to the recordset
      pSet->MoveNext();

      pSet->MoveLast();
      pSet->GetRecordCount();//something like this

      Hope it helps


      The statement below is true.
      The statement above is false.

      S D 2 Replies Last reply
      0
      • M Monty2

        Been a long time since i programmed in ADO but IIRC you don't need to

        while (!pSet->IsEOF()) //pSet is a pointer to the recordset
        pSet->MoveNext();

        pSet->MoveLast();
        pSet->GetRecordCount();//something like this

        Hope it helps


        The statement below is true.
        The statement above is false.

        S Offline
        S Offline
        SWDevil
        wrote on last edited by
        #3

        Nope - I tried that and it doesn't work :) It gives 1 record as the result...

        P 1 Reply Last reply
        0
        • S SWDevil

          Nope - I tried that and it doesn't work :) It gives 1 record as the result...

          P Offline
          P Offline
          psramu
          wrote on last edited by
          #4

          U just move to the first record and now loop until the end of records take a variable and increment it inside the loop with MOVENEST() function. the variable holds the total records count

          S 1 Reply Last reply
          0
          • P psramu

            U just move to the first record and now loop until the end of records take a variable and increment it inside the loop with MOVENEST() function. the variable holds the total records count

            S Offline
            S Offline
            SWDevil
            wrote on last edited by
            #5

            I don't understand what you mean... can you write some code maybe? and by the way, I would rather not use a loop - it's not very efficient... I need an efficient solution :)

            1 Reply Last reply
            0
            • M Monty2

              Been a long time since i programmed in ADO but IIRC you don't need to

              while (!pSet->IsEOF()) //pSet is a pointer to the recordset
              pSet->MoveNext();

              pSet->MoveLast();
              pSet->GetRecordCount();//something like this

              Hope it helps


              The statement below is true.
              The statement above is false.

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              Monty2 wrote:

              pSet->GetRecordCount();//something like this

              This will simply give you the “high water mark” — the highest-numbered record yet seen as the user moves through the records.


              "The largest fire starts but with the smallest spark." - David Crow

              S 1 Reply Last reply
              0
              • S SWDevil

                hi, I have a recordset which contains the results of a query, and I want to get the number of records in the recordset. I tried to use the GetRecordCount function, but I understood that I have to go through the whole recordset in order to get the number of records. So I have this loop in my code which is very inefficient: while (!pSet->IsEOF()) //pSet is a pointer to the recordset pSet->MoveNext(); I thouhgt to do an SQL query using COUNT, but it requires a simple query (something like: SELECT COUNT (*) FROM Table1 ) and my query is not a simple one. It contains selection of a number of records, and Inner joins of tables and an ORDER BY clause. Is there another way to get the number of records in a recordset? Any suggestions?

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #7

                SWDevil wrote:

                Is there another way to get the number of records in a recordset?

                You can use JOINs with the COUNT() clause. Simply put COUNT() around the columns that you are querying.


                "The largest fire starts but with the smallest spark." - David Crow

                S 1 Reply Last reply
                0
                • D David Crow

                  Monty2 wrote:

                  pSet->GetRecordCount();//something like this

                  This will simply give you the “high water mark” — the highest-numbered record yet seen as the user moves through the records.


                  "The largest fire starts but with the smallest spark." - David Crow

                  S Offline
                  S Offline
                  SWDevil
                  wrote on last edited by
                  #8

                  Yeah - I know. That's why I need another solution... Is there another way to get the number of records in a recordset other than going through the recordset with a loop or using COUNT? -- modified at 9:23 Tuesday 16th May, 2006

                  D 1 Reply Last reply
                  0
                  • D David Crow

                    SWDevil wrote:

                    Is there another way to get the number of records in a recordset?

                    You can use JOINs with the COUNT() clause. Simply put COUNT() around the columns that you are querying.


                    "The largest fire starts but with the smallest spark." - David Crow

                    S Offline
                    S Offline
                    SWDevil
                    wrote on last edited by
                    #9

                    I tried doing that (in SQL server) and it doesn't work...

                    D 1 Reply Last reply
                    0
                    • S SWDevil

                      I tried doing that (in SQL server) and it doesn't work...

                      D Offline
                      D Offline
                      David Crow
                      wrote on last edited by
                      #10

                      Ok, so just use COUNT(*), leaving the JOIN and WHERE clauses intact.


                      "The largest fire starts but with the smallest spark." - David Crow

                      1 Reply Last reply
                      0
                      • S SWDevil

                        Yeah - I know. That's why I need another solution... Is there another way to get the number of records in a recordset other than going through the recordset with a loop or using COUNT? -- modified at 9:23 Tuesday 16th May, 2006

                        D Offline
                        D Offline
                        David Crow
                        wrote on last edited by
                        #11

                        SWDevil wrote:

                        Is there another way to get the number of records in a recordset other than going through the recordset with a loop or using COUNT?

                        Have you considered/tried a stored procedure?


                        "The largest fire starts but with the smallest spark." - David Crow

                        S 1 Reply Last reply
                        0
                        • D David Crow

                          SWDevil wrote:

                          Is there another way to get the number of records in a recordset other than going through the recordset with a loop or using COUNT?

                          Have you considered/tried a stored procedure?


                          "The largest fire starts but with the smallest spark." - David Crow

                          S Offline
                          S Offline
                          SWDevil
                          wrote on last edited by
                          #12

                          Ummmm - no. don't really know what that is...

                          D 1 Reply Last reply
                          0
                          • S SWDevil

                            Ummmm - no. don't really know what that is...

                            D Offline
                            D Offline
                            David Crow
                            wrote on last edited by
                            #13

                            If you are going to be using SQL Server for anything othern than a novelty, I consider it a must-have in the developer's toolbox. http://www.awprofessional.com/articles/article.asp?p=25288&rl=1[^] http://databases.about.com/od/sqlserver/l/aastoredprocs.htm[^] http://www.sqlteam.com/item.asp?ItemID=563[^] http://www.codeproject.com/database/mssqltutorial.asp[^]


                            "The largest fire starts but with the smallest spark." - David Crow

                            S 1 Reply Last reply
                            0
                            • D David Crow

                              If you are going to be using SQL Server for anything othern than a novelty, I consider it a must-have in the developer's toolbox. http://www.awprofessional.com/articles/article.asp?p=25288&rl=1[^] http://databases.about.com/od/sqlserver/l/aastoredprocs.htm[^] http://www.sqlteam.com/item.asp?ItemID=563[^] http://www.codeproject.com/database/mssqltutorial.asp[^]


                              "The largest fire starts but with the smallest spark." - David Crow

                              S Offline
                              S Offline
                              SWDevil
                              wrote on last edited by
                              #14

                              Thanks - I'll look at the links :)

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups