select name in bank with C#

Asked

Viewed 880 times

-1

I am developing an application in C# windows form in 3 layers. My problem is: I have a datagridview to receive data from the bank and a textbox to be typed the name I want to search for in the bank, but as I am doing in 3 layers I am not using the textbox in my sql query that I normally see on the internet like this:

"select * from table Where name like '%" + txt_name +"%'"

I wanted to know how I do this consultation, I tried several ways but without result.

Bank code`

    public DataTable Consultar(DadosPessoa nome)
    {
        try
        {
            conexao = new MySqlConnection(conecta_db);

            MySqlCommand comando = new MySqlCommand("SELECT * FROM funcionario WHERE nome_func like @nome", conexao);
            comando.Parameters.AddWithValue("@nome", nome.Nome_Func);

            MySqlDataAdapter da = new MySqlDataAdapter(comando);

            DataTable dt = new DataTable();

            da.Fill(dt);

            return dt;
        }
        catch (Exception erro)
        {

            throw erro;
        }
    }`

This is my code of the class that accesses the bank:

public DataTable Consulta_Dao(string nome)
{
    try
    {
        dao = new Classe_Dao();
        DataTable dt = new DataTable();

        dt = dao.Consultar(nome);

        return dt;
    }
    catch (Exception erro)
    {

        throw erro;
    }
}

This is the button you search for:

private void btn_pesquisar_Click(object sender, EventArgs e, string nome)
{
    try
    {
        ClasseBll bll = new ClasseBll();

       nome = txt_pesquisar.Text;

        dg_dados.DataSource = bll.Consulta_Dao(nome);

    }
    catch (Exception erro)
    {

        throw erro;
    }
}
  • 1

    And what’s the problem or mistake?

  • 1

    Tell us what it is, and where the error occurs. Because analyzing the code like this on top of it, you don’t seem to have problems. It would only tell you to change the query, not concatenate it, but use the.Parameters.Addwithvalue("name", name);

  • the problem is that when I click the button to search it does not answer me at all.

  • It looks fine. To me, it’s the query that isn’t finding anyone with that name. It shows the code of "Query".

  • I’ve already edited and entered the bank code

  • @Aéciocleysson Also check if the button has the event Click for the method btn_search_Click or if it is empty. This check can be done in the . design file or depending on the editor, in the events part of the button.

Show 1 more comment

2 answers

0

Consultar expects a parameter of type DadosPessoa, being that you’re passing nome which is a string.

0

I imagine the error is in the parameter passage for the query. Try this way.

public DataTable Consultar(string nome)
{
    try
    {
        conexao = new MySqlConnection(conecta_db);

        MySqlCommand comando = new MySqlCommand("SELECT * FROM funcionario WHERE nome_func like @nome", conexao);
        comando.Parameters.AddWithValue("@nome", nome);

        MySqlDataAdapter da = new MySqlDataAdapter(comando);

        DataTable dt = new DataTable();

        da.Fill(dt);

        return dt;
    }
    catch (Exception erro)
    {

        throw erro;
    }
}`

Browser other questions tagged

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