Recording value of a variable in a table

Asked

Viewed 42 times

1

I’m trying to save the data of 2 variables in a table, but when I look at the table in the database is recorded the values as 0.

       public void inserir(List<Conta>list)
    {
        List<Conta> lista = new List<Conta>();

        lista = list;

        try
        {
            MySqlConnection onclick = new MySqlConnection("server = localhost; port=3306; User Id=root; database = 2.0 banco; password =");
            onclick.Open();



            MySqlCommand onCmd = new MySqlCommand("INSERT INTO CONTA(nome, cpf) VALUES (nome = @nome, cpf = @cpf)", onclick);


            onCmd.Parameters.AddWithValue("@nome",lista[lista.Count-1].GetNome()); 
            onCmd.Parameters.AddWithValue("@cpf", lista[lista.Count - 1].GetCpf());               
            onCmd.ExecuteNonQuery();
            MessageBox.Show("Cadastrado com sucesso: " + lista[lista.Count - 1].GetCpf());
        }
        catch(Exception erro)
        {

            MessageBox.Show("Erro ao cadastrar: " + erro);

        }

    }


}

1 answer

2


The syntax used is a mixed insertion and update, which is wrong, the correct is:

"INSERT INTO CONTA(nome, cpf) VALUES (@nome, @cpf)"

I put in the Github for future reference.

There’s some pretty weird stuff, not recommended in the code or with potential for error, so there might be something else. And you do not save variables in the database, save values, they may be originally stored in the variable.

  • It worked! Thank you.

Browser other questions tagged

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