Way to get all results of SQL Command query

Asked

Viewed 45 times

0

I am trying to select two columns of a table via sql command by running a precedent.

Code :

String consult = Convert.ToString(command.ExecuteScalar());

Procedure :

BEGIN
        SELECT Value, TimeStamp
        FROM AVL_Ignition
        WHERE TimeStamp = (SELECT MAX(TimeStamp) FROM AVL_Ignition)
        IF @@ROWCOUNT = 0
            select -1

END

I am only getting the result of Value because it is the first affected. Is there any way to get all the elements results of the query ? I already looked at the methods of the sql command class but found nothing promising.

1 answer

1


The ExecuteScalar takes only the first column of the first row.

To list the records, use the ExecuteReader().

For example:

SqlDataReader reader = command.ExecuteReader();

while (reader.Read())
{
    string valor = reader["Value"].ToString();
    DateTime data = Convert.ToDateTime(reader["TimeStamp"]);

    // fazer o que quiser com os valores
}

Browser other questions tagged

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