Connection error Mysql C #

0

At the moment of executing a select to connect to a Mysql database I get the warning of Mysql connection can not be open the code fragment I use is the following:

private string mensaje;
        private string usuario;
        private string clave;
        private string tipo;

        public string Mensaje
        {
            get { return mensaje; }
            set { mensaje = value; }
        }
        public string Usuario
        {
            get { return usuario; }
            set { usuario = value; }
        }
        public string Clave
        {
            get { return clave; }
            set { clave = value; }
        }

        public string Tipo
        {
            get { return tipo; }
            set { tipo = value; }
        }

        public bool Verificar()
        {
            bool resultado = false;

            var comando = new MySqlCommand("SELECT * FROM lgn WHERE CARGO = @cargo and USUARIO = @usuario and PASS = @pass");
            comando.Parameters.AddWithValue("@cargo", tipo);
            comando.Parameters.AddWithValue("@usuario", usuario);
            comando.Parameters.AddWithValue("@pass", clave);

            CONEXION.ObtenerConexion();
            MySqlDataReader ejecuta = comando.ExecuteReader();
            if (ejecuta.Read())
            {
                resultado = true;
                mensaje = "Su Logueo Fue Ingresado Correctamente \n \n               Bienvenido al Sistema ";
            }
            else
            {
                mensaje = "         Excedio el Limite de Intentos al Sistema \n \nEspere unos Minutos y Ingrese Su Logueo Otra Vez";
            }
            return resultado;
        }

public static MySqlConnection ObtenerConexion()
        {
            MySqlConnection conectar = new MySqlConnection("server=localhost; port=xxxx; database=tabernac_vverdadera; Uid=xxx; pwd=xxxxxx;");
            conectar.Open();
            return conectar;
        }
    
asked by Julio Martinez 21.08.2017 в 23:48
source

1 answer

1

You have to assign the result of calling CONEXION.ObtenerConexion() to MySqlCommand so you can use it.

After reading the results, do not forget to close the connection with .Close()

public bool Verificar()
{
    bool resultado = false;

    var conexion = CONEXION.ObtenerConexion();
    var comando = new MySqlCommand("SELECT * FROM lgn WHERE CARGO = @cargo and USUARIO = @usuario and PASS = @pass", conexion);
    comando.Parameters.AddWithValue("@cargo", tipo);
    comando.Parameters.AddWithValue("@usuario", usuario);
    comando.Parameters.AddWithValue("@pass", clave);

    MySqlDataReader ejecuta = comando.ExecuteReader();
    if (ejecuta.Read())
    {
        resultado = true;
        mensaje = "Su Logueo Fue Ingresado Correctamente \n \n               Bienvenido al Sistema ";
    }
    else
    {
        mensaje = "         Excedio el Limite de Intentos al Sistema \n \nEspere unos Minutos y Ingrese Su Logueo Otra Vez";
    }

    conexion.Close();
    return resultado;
}

Better yet you can improve the above code if you use the keyword using that will automatically call the .Dispose() method of the classes that implement IDisposable as MySqlConnection and MySqlDataReader . This will also close the connection automatically.

public bool Verificar()
{
    using (var conexion = CONEXION.ObtenerConexion())
    {
        using (var comando = new MySqlCommand("SELECT * FROM lgn WHERE CARGO = @cargo and USUARIO = @usuario and PASS = @pass", conexion))
        {
            comando.Parameters.AddWithValue("@cargo", tipo);
            comando.Parameters.AddWithValue("@usuario", usuario);
            comando.Parameters.AddWithValue("@pass", clave);

            using (var reader = comando.ExecuteReader())
            {
                if (reader.Read())
                {
                    mensaje = "Su Logueo Fue Ingresado Correctamente \n \n               Bienvenido al Sistema ";
                   return true;
                }
                else
                {
                    mensaje = "         Excedio el Limite de Intentos al Sistema \n \nEspere unos Minutos y Ingrese Su Logueo Otra Vez";
                    return false;
                }
            }
        }
    }
}
    
answered by 22.08.2017 / 00:28
source