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.
– Erico Souza
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).
– Bacco
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
.– Bacco
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.
– Erico Souza
I meant to test hardcoded in Visual Studio, putting a comment on the line
// mysql.Comando.Parameters.AddWithValue...
and exchangingand m.med_cod = @CODIGO
forand m.med_cod = 12
(put an existing code in the base). So you can better surround the problem.– Bacco
I performed the test, it didn’t work...
– Erico Souza
can post mysql code.Executarcomando()?
– kuwakino
@user2004054 added
– Erico Souza
@Bacco added more code details to the question
– Erico Souza