Query Mysql in Visual Studio does not return all data

Asked

Viewed 704 times

0

I have a problem in Visual Studio, where in my database access class, I have a query that makes a query in my Mysql database, and returns only the last result. Therefore, when I test this same query directly in the Mysql database, using tools like Phpmyadmin, Mysql Workbench, etc. The database returns me, all the results, which should return, which in case are 29 items.

Follow the C# code of my database access method,

    public DataTable ObterListadePrecos(int codigoMedico)
    {
        Mysql mysql = new Mysql();
        try
        {
            mysql.Comando.CommandText = @"select e.codigo_anatomico, em.medico as cod, e.nome, concat(g.nome, ' > ',  s.nome) as grupo, e.valor as valorCheio, t.codigo as codTabela, t.desconto,
                                    (valor - (valor * t.desconto / 100) ) as valorComDesconto,              
                                    (valor * (select case 
                                                    when desconto = 0 
                                                    then 100 
                                                    else desconto 
                                                    end) * t.usuario_convencional /100 / 100 ) as uconvencional,              
                                    (valor * (select case 
                                                    when desconto = 0 
                                                    then 100 
                                                    else desconto 
                                                    end) * t.convenio_convencional /100 / 100 ) as sconvencional,              
                                    (valor * (select case 
                                                    when desconto = 0 
                                                    then 100 
                                                    else desconto 
                                                    end) * t.usuario_master /100 / 100 ) as umaster,              
                                    (valor * (select case 
                                                    when desconto = 0 
                                                    then 100 
                                                    else desconto 
                                                    end) * t.convenio_master /100 / 100 ) as smaster
                                    from exame_medico em, exames e, subgrupo_exame s, grupo_exame g, tabela_preco t, medico m
                                    where e.codigo_subgrupo = s.codigo
                                    and em.subgrupo = s.codigo
                                    and s.grupo_codigo = g.codigo
                                    and m.med_tabela = t.codigo
                                    and em.medico = m.med_cod
                                    and m.med_cod = @CODIGO
                                    order by e.nome desc";

            mysql.Comando.Parameters.AddWithValue("@CODIGO", codigoMedico);

            return mysql.ExecutarComando();
        }
        catch(MysqlException e)
        {
            //tratamento de minha exceptions
        }
    }

Parte do código onde monto a Grid para exibição dos dados:

    gridView.DataSource = minhaClasse.ObterListadePrecos(valor);
    gridView.DataBind()

**[UPDATE]**Código do método executar comando

public DataTable ExecutarComando()
        {
            if (_comando.CommandText == string.Empty)
                throw new Exception("Comando sem instrução SQL.");

            DataTable dt = new DataTable();

            try
            {
                if (_conexao.State == ConnectionState.Closed)
                    _conexao.Open();

                MySqlDataReader dr = _comando.ExecuteReader();

                dt.Load(dr);
            }
            catch (Exception ex)
            {

                if (_transacao != null)
                {
                    _transacao.Rollback();
                    _transacao = null;
                }

                if (_conexao.State == ConnectionState.Open)
                    _conexao.Close();

                throw new Exception("Mysql.ExecutarComando.", ex);
            }
            finally
            {
                if (_gerenciarConexaoAutomatica)
                    _conexao.Close();
            }

            return (dt);
        }
  • @Bacco the problem is, if I make one mysql.Executarcomando().rows.Count(); (method used to count the datatable lines), it is returning me only 1. For this reason I believe the problem is here maybe.

  • I asked to have a more complete view of the whole thing, in case you were using some loop. Does this same query return multiple results when you manually test? Try trading @CODIGO for an already tested numeric value directly in Commandtext instead of using Addwithvalue (test only, to see if the problem is in attribution or SQL).

  • If it works with a hardcoded value, take a look at this too, about Allowuservariables (to accept @... ): http://forums.mysql.com/read.php?38,581862,581943#msg-581943 , because suddenly this single line being returned is just some with med_cod = 0.

  • Well, I’ll test here, the problem is that in other queries, it works well @... and in the database, I tested hardcoded, with the value of the variable that is in @codigoMedico, and it returns the 29 records, but when I run the application in Visual Studio, it comes only the last. I’m beginning to believe it might be a Mysql nector bug.

  • I meant to test hardcoded in Visual Studio, putting a comment on the line // mysql.Comando.Parameters.AddWithValue... and exchanging and m.med_cod = @CODIGO for and m.med_cod = 12 (put an existing code in the base). So you can better surround the problem.

  • I performed the test, it didn’t work...

  • 1

    can post mysql code.Executarcomando()?

  • @user2004054 added

  • @Bacco added more code details to the question

Show 4 more comments

1 answer

1


Try the function: (I don’t think it will work with the parameters. I don’t know how to do with parameters, because I always provide SQL ready, or use Fluent Nhibernate. While providing the ready SQL)

public DataTable ExecutarComando()
        {
            if (_comando.CommandText == string.Empty)
                throw new Exception("Comando sem instrução SQL.");

            DataSet DS = new DataSet();
            DataTable dt = new DataTable();

            try
            {
                if (_conexao.State == ConnectionState.Closed)
                    _conexao.Open();    

                using (var MyDataAdapter = new System.Data.OleDb.OleDbDataAdapter(_comando.CommandText, _conexao))
                {
                    MyDataAdapter.Fill(dt, "TABELA");
                }

                //MySqlDataReader dr = _comando.ExecuteReader();
                //dt.Load(dr);    
            }
            catch (Exception ex)
            {    
                if (_transacao != null)
                {
                    _transacao.Rollback();
                    _transacao = null;
                }

                if (_conexao.State == ConnectionState.Open)
                    _conexao.Close();

                throw new Exception("Mysql.ExecutarComando.", ex);
            }
            finally
            {
                if (_gerenciarConexaoAutomatica)
                    _conexao.Close();
            }

            //return (dt);
            return (DS.Tables[0]);
        }

Browser other questions tagged

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