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. Web Development
  3. ASP.NET
  4. Record count for SQLDataReader

Record count for SQLDataReader

Scheduled Pinned Locked Moved ASP.NET
databasequestion
5 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.
  • V Offline
    V Offline
    VK Link
    wrote on last edited by
    #1

    Hi - this was so simple in ASP, where we could get a record count of the recordset. How would we do the same in using SQLDataReader? The sql query is executed using sqlcommand and using datareader to read the records - need a count of total number of records retrieved. Thanks.

    A L V 3 Replies Last reply
    0
    • V VK Link

      Hi - this was so simple in ASP, where we could get a record count of the recordset. How would we do the same in using SQLDataReader? The sql query is executed using sqlcommand and using datareader to read the records - need a count of total number of records retrieved. Thanks.

      A Offline
      A Offline
      Anonymous
      wrote on last edited by
      #2

      hi there, you can use the follwing codes for examples which i've been using: static int rows; sqlString="SELECT * FROM whatever WHERE condition; sqlCom = new SqlCommand(sqlString,quizConn); sqlCom.Connection.Open(); SqlDataReader tryReader = sqlCom.ExecuteReader(); while(tryReader.Read() == true) { rows++; }

      1 Reply Last reply
      0
      • V VK Link

        Hi - this was so simple in ASP, where we could get a record count of the recordset. How would we do the same in using SQLDataReader? The sql query is executed using sqlcommand and using datareader to read the records - need a count of total number of records retrieved. Thanks.

        L Offline
        L Offline
        Luis Alonso Ramos
        wrote on last edited by
        #3

        I suppose that you need the count in advance of reading every record (as the other reply suggests.) You can't do that using a data reader, since you get each record as it is read from the database. What I'd suggest is to use a SqlDataAdapter to get all the records inside a DataTable, and then you have the count.

        SqlCommand cmd = new SqlCommand("SELECT * FROM MyTable", conn);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
         
        // dt.Rows.Count has now the number of records read
         
        foreach(DataRow row in dt.Rows)
        {
           // Use row["Field"] as you would use dataReader["Field"]
        }
        

        I hope this helps! -- LuisR


        Luis Alonso Ramos Intelectix - Chihuahua, Mexico Not much here: My CP Blog!

        The amount of sleep the average person needs is five more minutes. -- Vikram A Punathambekar, Aug. 11, 2005

        1 Reply Last reply
        0
        • V VK Link

          Hi - this was so simple in ASP, where we could get a record count of the recordset. How would we do the same in using SQLDataReader? The sql query is executed using sqlcommand and using datareader to read the records - need a count of total number of records retrieved. Thanks.

          V Offline
          V Offline
          Vitaly Tomilov
          wrote on last edited by
          #4

          SQLCommand myCommand = new SQLCommand("SELECT COUNT(*) FROM MyTable", MyConnection); MyConnection.Open(); int nRecords = myCommand.ExecuteScalar(); // get the number of records in the table;

          V 1 Reply Last reply
          0
          • V Vitaly Tomilov

            SQLCommand myCommand = new SQLCommand("SELECT COUNT(*) FROM MyTable", MyConnection); MyConnection.Open(); int nRecords = myCommand.ExecuteScalar(); // get the number of records in the table;

            V Offline
            V Offline
            VK Link
            wrote on last edited by
            #5

            We then could also set the a datareader - like myReader = myCommand.ExecuteReader - can we set both of them? A ExecuteReader and ExecuteScalar? Thanks.

            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