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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Count the number of rows with sqldatareader

Count the number of rows with sqldatareader

Scheduled Pinned Locked Moved C#
question
5 Posts 2 Posters 1 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.
  • G Offline
    G Offline
    Genbox
    wrote on last edited by
    #1

    As the subject says, how do i count the number of rows with sqldatareader ?

    J 1 Reply Last reply
    0
    • G Genbox

      As the subject says, how do i count the number of rows with sqldatareader ?

      J Offline
      J Offline
      J4amieC
      wrote on last edited by
      #2

      Return the @@ROWCOUNT from the query as an output parameter

      G 1 Reply Last reply
      0
      • J J4amieC

        Return the @@ROWCOUNT from the query as an output parameter

        G Offline
        G Offline
        Genbox
        wrote on last edited by
        #3

        I new to C# and mssql. this is my code: SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString); SqlDataReader rdr = null; conn.Open(); SqlCommand cmd = new SqlCommand("SELECT count(*) FROM training WHERE userid='genbox'", conn); rdr = cmd.ExecuteReader(); while (rdr.Read()) { Response.Write(#WANT COUNTER HERE#); } rdr.Close(); conn.Close(); I figured that something like SELECT count(*) FROM... could be used, but if @@ROWCOUNT is better, i will use that instead.

        J 1 Reply Last reply
        0
        • G Genbox

          I new to C# and mssql. this is my code: SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString); SqlDataReader rdr = null; conn.Open(); SqlCommand cmd = new SqlCommand("SELECT count(*) FROM training WHERE userid='genbox'", conn); rdr = cmd.ExecuteReader(); while (rdr.Read()) { Response.Write(#WANT COUNTER HERE#); } rdr.Close(); conn.Close(); I figured that something like SELECT count(*) FROM... could be used, but if @@ROWCOUNT is better, i will use that instead.

          J Offline
          J Offline
          J4amieC
          wrote on last edited by
          #4

          Oh, ok two ways - both exactly the same, but one is a tonne more readable. You are returning the count as the first column in a select list so you can just go: while (rdr.Read()) { Response.Write(rdr[0].ToString()); // rdr[0] refers to the first column in the resultset } However, you can make this much more readable by aliasing the column as a name SqlCommand cmd = new SqlCommand("SELECT count(*) AS TrainingCount FROM training WHERE userid='genbox'", conn); now: while (rdr.Read()) { Response.Write(rdr["TrainingCount"].ToString()); // no longer need to know which column number has the count - we refer by name } HTH! -- modified at 7:22 Friday 16th December, 2005

          G 1 Reply Last reply
          0
          • J J4amieC

            Oh, ok two ways - both exactly the same, but one is a tonne more readable. You are returning the count as the first column in a select list so you can just go: while (rdr.Read()) { Response.Write(rdr[0].ToString()); // rdr[0] refers to the first column in the resultset } However, you can make this much more readable by aliasing the column as a name SqlCommand cmd = new SqlCommand("SELECT count(*) AS TrainingCount FROM training WHERE userid='genbox'", conn); now: while (rdr.Read()) { Response.Write(rdr["TrainingCount"].ToString()); // no longer need to know which column number has the count - we refer by name } HTH! -- modified at 7:22 Friday 16th December, 2005

            G Offline
            G Offline
            Genbox
            wrote on last edited by
            #5

            Thanks, it sort of works. :S Cant i use the same SqlCommand to extract data from the tabel ? if i exec this: while (rdr.Read()) { Response.Write(rdr["TrainingCount"].ToString()); } then it returns the number of rows like it should, but if i read any data from the table, then it returns an error. - this is the code: while (rdr.Read()) { Response.Write(rdr["TrainingCount"].ToString()); Response.Write(rdr[1]); } And this is the error: System.IndexOutOfRangeException: Index was outside the bounds of the array. Do i really have to make 2 SqlCommands, one that counts and one that i can get data from? that dosent seem right to me.

            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