How to Add Record Count on Existing Code

FRANK LUJAN 20 Reputation points
2025-01-09T21:11:25.2866667+00:00

What is easiest way to add Record count for this code:

try

{

String connectionString = "Data Source=XPSSLOWPC;Initial Catalog=Movies;Integrated Security=True;";

using (SqlConnection connection = new(connectionString))

{

    connection.Open();

    String sql = "select Title, Year, Format, Length, Video, Audio from Info where Format = 'Ultra HD' order by Title";

    using (SqlCommand command = new(sql, connection))

    {

        using (SqlDataReader reader = command.ExecuteReader())

        {

            while (reader.Read())

            {

                MovieInfo movieinfo = new MovieInfo();

                {

                    movieinfo.Title = reader.GetString(0);

                    movieinfo.Year = reader.GetString(1);

                    movieinfo.Format = reader.GetString(2);

                    movieinfo.Length = reader.GetString(3);

                    movieinfo.Video = reader.GetString(4);

                    movieinfo.Audio = reader.GetString(5);

                };

                ListMovies.Add(movieinfo);

            }

        }

    }

}
```}

catch (Exception ex)

{

```yaml
Console.WriteLine("Exception: " + ex.ToString());
```}
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,185 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 69,121 Reputation points
    2025-01-10T01:09:19.28+00:00

    Adding a counter is the easiest:

    var recordCount = 0;
    String connectionString = "Data Source=XPSSLOWPC;Initial Catalog=Movies;Integrated Security=True;";
    
    using (SqlConnection connection = new(connectionString))
    {
        connection.Open();
        String sql = "select Title, Year, Format, Length, Video, Audio from Info where Format = 'Ultra HD' order by Title";
    
        using (SqlCommand command = new(sql, connection))
        {
            using (SqlDataReader reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    ++recordCount;
                    MovieInfo movieinfo = new MovieInfo();
                    {
                        movieinfo.Title = reader.GetString(0);
                        movieinfo.Year = reader.GetString(1);
                        movieinfo.Format = reader.GetString(2);
                        movieinfo.Length = reader.GetString(3);
                        movieinfo.Video = reader.GetString(4);
    
                        movieinfo.Audio = reader.GetString(5);
                    };
    
                    ListMovies.Add(movieinfo);
                }
            }
        }
    }
    

    Note: you could a “select @@rowcount” to the query and it would come in the second result set.


  2. Michael Taylor 55,841 Reputation points
    2025-01-10T17:12:35.7033333+00:00

    If you want to know how many records you read then you already have that information. You're adding the records to a list. However many items are in the list are the # of rows you read.

    using (SqlConnection...)
       using (SqlReader ...)
          while (...Read())  
             ...
    
    //How many did you read?
    int totalRecordsRead = ListMovies.Count;
    

    If the ListMovies isn't empty when you start adding records to it then just subtract that out of the calculation.

    int originalCount = ListMovies.Count;
    //read the data
    int totalRecordsRead = ListMovies.Count - originalCount;
    

    You don't really need to do anything special or modify your query at all.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.