Force a Sqldatareader result

Asked

Viewed 178 times

0

I have the following method:

        public ClassCardapio MontaCardapioEdit(string periodo, int tipo)
    {
        ClassCardapio cardapio = new ClassCardapio(_stringconexao);

        _conexao.Open();

        var sql = $"SELECT " +
                    "C.DESC_TIPO  as 'COMPOSIÇÃO'" +
                    ",B.SEGUNDA " +
                    ",B.TERCA " +
                    ",B.QUARTA " +
                    ",B.QUINTA " +
                    ",B.SEXTA " +
                    "FROM CARDAPIO A " +
                    "JOIN CARDAPIO_ITEM B ON A.ID = B.ID_CARDAPIO " +
                    "JOIN CARDAPIO_TIPO C ON B.TIPO = C.ID " +
                    "WHERE A.ID = '" + periodo + "' " +
                    "AND B.TIPO = '" + tipo + "' " +
                    "ORDER BY B.ID asc ";
        var cmd = new SqlCommand(sql, _conexao);
        SqlDataReader dr = cmd.ExecuteReader();

        while (dr.Read())
        {

            cardapio.segunda = dr["SEGUNDA"].ToString();
            cardapio.terca = dr["TERCA"].ToString();
            cardapio.quarta = dr["QUARTA"].ToString();
            cardapio.quinta = dr["QUINTA"].ToString();
            cardapio.sexta = dr["SEXTA"].ToString();

        }

        _conexao.Close();

        return cardapio;
    }

The result of it:

inserir a descrição da imagem aqui

Through its result I am mounting several textbox, the problem is that this select returns 3 lines, because the "type" of these lines are the same, is there any way to force which line of the select will be displayed in each textbox? By default it is always showing the last line of the 3.

1 answer

1


The instruction While performs the task, until the condition is false.

Only your case dr.Read(), causes every loop to change the reading line of your DataTable, shall be executed until nay there are more lines.

Since this is being returned 3 lines of the query, the loop is executed 3 times, thus returning the value of the last line.

I can’t guarantee it’s the best alternative, but by putting a break at the end of the while, will ensure that it only runs once.

Or, we can withdraw the instruction While, that will make the system read only the first line..

  while (dr.Read())
    {

        cardapio.segunda = dr["SEGUNDA"].ToString();
        cardapio.terca = dr["TERCA"].ToString();
        cardapio.quarta = dr["QUARTA"].ToString();
        cardapio.quinta = dr["QUINTA"].ToString();
        cardapio.sexta = dr["SEXTA"].ToString();

        break;
    }

or

        dr.Read();

        cardapio.segunda = dr["SEGUNDA"].ToString();
        cardapio.terca = dr["TERCA"].ToString();
        cardapio.quarta = dr["QUARTA"].ToString();
        cardapio.quinta = dr["QUINTA"].ToString();
        cardapio.sexta = dr["SEXTA"].ToString();

To learn more about While

  • Using Break solved my problem, thank you very much!

  • @Raphaelnobre I ask you to pay attention to the use of break for, break without being in a condition if goes counter-hand with the use while..

Browser other questions tagged

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