Receiving and handling data from a query

Asked

Viewed 73 times

1

I would like to know how to manipulate the data of a query. I have a method that performs a Select, and I’m creating a List to store the results, and return this list. But how can I use this data out of the way?

Follows:

public List<Results> GetInfo(long id)
        {
            var Id = id;
            using (var conn = DatabaseUtil.GetConnection())
            using (var cmd = conn.CreateCommand())
            {
                cmd.CommandText = $@"SELECT
                                            cb.id AS Id,
                                            p.first_name AS FirstName, 
                                            p.last_name AS LastName,  
                                            cb.date AS Date         
                                        FROM
                                            st_compania cb
                                            JOIN st_pessoa p ON (p.id = cb.compania_id)
                                        WHERE
                                            cb.id = @Id

                cmd.Parameters.Add("@Id", SqlDbType.BigInt).Value = Id;

                var resultList = new List<Results>();

                using (var dr = cmd.ExecuteReaderWithRetry(DatabaseUtil.RetryPolicy))
                {
                    while (dr.Read())
                    {
                        resultList.Add(new Results
                        {
                          Id = Convert.ToInt64(dr["Id"]),
                          FirstName = Convert.ToString(dr["FirstName"]),
                          LastName = Convert.ToString(dr["LastName"]),
                          Date = Convert.ToDateTime(dr["Date"])
                        });
                    }
                }
                return resultList;
            }

        }

Well, Select works, but how can I use these results I collected, let’s say on a page, where I want to display the person’s Name and Surname and the date on a Label? I’d have to do something like this:

lblResult.Text = $"Nome: {resultList[1]}, Sobrenome: {resultList[2]} com a data de: {resultList[3]}."
  • 1

    but you already return the List<Results>, that’s not enough?

1 answer

3


var resultList= GetInfo(1)
foreach (var result in resultList)
{
  Console.WriteLine(result.FirstName);
  Console.WriteLine(result.Date);
}
  • 1

    Foreach had crossed my mind, but I had forgotten about it. Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.