MySql ExecuteReader does not bring results [C #]

0

good, I have the query, I am with a small project in C #, but I have the problem that a query that when executing it in mysql brings me the results correctly but when it is executed in my code, it recognizes me that it returns a row, but when I run the .read () it does not return anything, and if I check what it returns, it shows me the following message

  

enumeration yielded no results

It is clear that the parameters are passed correctly, but that simply fails, and when I review the non-public parameters, I find the resultset with the values I was looking for

ColumnsDB and queryes is a resource file to replace the names of the columns

query used:

Select * FROM usuarios WHERE username = '{0}' AND password = '{1}'

the code in question is this:

string conex = Conn.Connexion();
        MySqlConnection con = new MySqlConnection(conex);
        try
        {
            MySqlCommand comando = new MySqlCommand();
            comando.CommandText = string.Format(queryes.Login, txtUsuario.Text, txtPasswrd.Text);
            comando.CommandType = CommandType.Text;
            comando.Connection = con;
            con.Open();
            var reader = comando.ExecuteReader();
            var list = new List<UsuarioEntity>();
            if (reader.HasRows)
            {
                while (reader.Read())
                {

                    list.Add(new UsuarioEntity
                    {
                        User = (string)reader[ColumnasDB.username],
                        Nombre = (string)reader[ColumnasDB.nombre],
                        Apellido = (string)reader[ColumnasDB.apellido],
                        Matricula = (int)reader[ColumnasDB.matricula],
                        Perfil = (string)reader[ColumnasDB.perfil],
                        Activo = (bool)reader[ColumnasDB.isactive],
                    });
                }
            }

            if (reader.ToString() != string.Empty)
            {
                UsuarioEntity user = new UsuarioEntity();
                user.Nombre = txtUsuario.Text;
                user.Perfil = reader.ToString();

                MessageBox.Show("ConexionAbiertaCorrectamente");
            }

            con.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }

version of .Net 4.6

    
asked by Pablo Ezequiel Ferreyra 24.09.2017 в 01:54
source

1 answer

0

You should use parameters in your code and not the string.Format() , your code should look like this

try
{
    string conex = Conn.Connexion();
    using(MySqlConnection con = new MySqlConnection(conex))
    {
        con.Open();

        string query = "SELECT * FROM Tabla WHERE usuario = ?usuario AND password = ?password";
        MySqlCommand comando = new MySqlCommand(query, con);
        comando.Parameters.AddWithValue("?usuario", txtUsuario.Text);
        comando.Parameters.AddWithValue("?password", txtPasswrd.Text);

        var reader = comando.ExecuteReader();
        var list = new List<UsuarioEntity>();

        while (reader.Read())
        {

            list.Add(new UsuarioEntity
            {
                User = (string)reader[ColumnasDB.username],
                Nombre = (string)reader[ColumnasDB.nombre],
                Apellido = (string)reader[ColumnasDB.apellido],
                Matricula = (int)reader[ColumnasDB.matricula],
                Perfil = (string)reader[ColumnasDB.perfil],
                Activo = (bool)reader[ColumnasDB.isactive],
            });
        }

    }
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}      

you will see the use of comando.Parameters.AddWithValue() is there where you assign the value

Also you do not need to validate the HasRows since otherwise there are records you will not enter the while

The using defines a safe scope to work with the connection, so you do not need to perform close

    
answered by 25.09.2017 в 15:59