Property Hasrows in C#

Asked

Viewed 117 times

-1

I’m making a SELECT mysql with inner join within my application C#

          MySqlCommand _comandoDados = new MySqlCommand(String.Format(
              "select EMGroot.id, EMGroot.lbs_net, EMGroot.lbs_gross_aai, EMGroot.price_brazil, sum(AAIest.qtd_venda) " +
              "from eaglemo4_eaglemotorsg.tb_products_root EMGroot " +
              "inner join eaglemo4_americai.tb_estoque AAIest " +
              "on AAIest.id_produto = EMGroot.id " +
              "where EMGroot.manupart = '" + txtManuPartInserir.Text + "'"), conexao_aai);

        MySqlDataReader Open_readerteste = _comandoDados.ExecuteReader();
        Open_readerteste.Read();

After getting a Reader I should throw the dice to some fields, but in order to avoid "DATA IS NULL" error, I create the condition HasRows :

   if(Open_readerteste.HasRows)
   {
     root_id = Open_readerteste.GetInt16(0)
     ....
   } 

but the result of the condition is always being TRUE even when there is no rows.
Can anyone tell if because of inner join the reader always consider data in the ? command or why Hasrows is always true ?

=================
Updating my doubt; Descrobri that in fact the HasRows is always true because of the field search sum(AAIest.qtd_venda). With the sum() tool Hasrows will always indicate that there are lines within the dataReader.

Is there any way to work this ?

  • if multiple lines... while (Open_readerteste.Read()) if it is a if (Open_readerteste.Read())

  • There is a similar question in Soen, in summary one of the answers suggests the use of MySqlCommand.ExecuteScalar.

  • There are several lines, the Reader I already opened as the code shows (Open_readerteste.Read();).. works perfectly when select finds data, but when it does not find that is where it should NOT enter the 'hasrows' condition it is entering and thus indicating date is null error.

  • I found that the problem is in the search for the "sum(Aaiest.qtd_sell)" field using SUM() Hasrows will always inform that there is a line within the Reset dataset. If anyone knows how to fix this !?

1 answer

1


Try including the clause having to secure lines only when SUM is greater than zero or some other counter is greater than zero.


   "select EMGroot.id, EMGroot.lbs_net, EMGroot.lbs_gross_aai, EMGroot.price_brazil, sum(AAIest.qtd_venda) " +
              "from eaglemo4_eaglemotorsg.tb_products_root EMGroot " +
              "inner join eaglemo4_americai.tb_estoque AAIest " +
              "on AAIest.id_produto = EMGroot.id " +
              "where EMGroot.manupart = '" + txtManuPartInserir.Text + "'
GROUP BY EMGroot.id, EMGroot.lbs_net, EMGroot.lbs_gross_aai, EMGroot.price_brazil
HAVING sum(AAIest.qtd_venda) > 0
"

  • Perfect, it worked perfectly. ... GROUP BY Emgroot.id HAVING sum(Aaiest.qtd_venda) > 0

Browser other questions tagged

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