select name in bank with C #

-1

I'm developing an application in C # windows form in 3 layers. My problem is: I have datagridview to receive the database data and a textbox to be typed the name I want to fetch in the database, but as I'm doing it in 3 layers I'm not using textbox in my sql query which I usually see on the internet like this:

  

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

I wanted to know how to do this query, I tried several forms but no results.

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 for 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 search button:

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;
    }
}
    
asked by anonymous 01.06.2017 / 00:20

2 answers

0

Consultar expects a parameter of type DadosPessoa , you are passing nome which is string .

    
03.06.2017 / 15:06
0

I imagine the error is in the parameter pass for the search. Try it that 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;
    }
}'
    
05.06.2017 / 18:26