Problem with catch in MySQL connection c #

0

Hello, I'm a beginner in programming and I'm having trouble connecting MySQL in C #, where the code does not check and goes into the catch action. Here is the code:

conexaoDataSet = new DataSet();
            conexao = new MySqlConnection("SERVER=127.0.0.1; DATABASE=tcc; UID=root; PASSWORD=;");
            try
            {
                conexao.Open();
                MySqlCommand verifica = new MySqlCommand("SELECT * FROM CMS_pilots WHERE nome='" + txt_login.Text + "' and senha ='" + txt_senha.Text + "'", conexao);
                bool resultado = verifica.ExecuteReader().HasRows;
                if (resultado == true)
                {
                    conexao.Close();
                    adm f2 = new adm();
                    f2.Show(this);
                    Hide();
                }
                else
                {
                    MessageBox.Show("Login inválido!");
                    conexao.Close();
                }
            }
            //erro acontece a partir de aqui
            catch
            {
                MessageBox.Show("Erro de conexão com o banco de dados");
                conexao.Close();
            }
        }
    }
}
    
asked by anonymous 25.04.2015 / 17:30

1 answer

2

Nayanne, your code is correct, you should not be able to connect to the bank, do the following test: try{ conexao.Open(); } catch{ MessageBox.Show("Não estabeleceu conexão com o banco de dados"); }

Really unable to connect?  Verify that you have already installed the MySQL Connector NET  Is your connection string correct?  Was the reference made? and was referenced correctly?

using MySql.Data.MySqlClient;
using System.Data;

Check this detailed example that shows you how to model the database, and how to connect to the database.

link

    
27.04.2015 / 13:42