How to Add Record Count on Existing Code

FRANK LUJAN 40 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());
```}
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Michael Taylor 60,161 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.


1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    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.


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.