Registering the value of a variable in a table

1

I'm trying to save data from 2 variables in a table, but when I look at the table in the database the values are recorded 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);

        }

    }


}
    
asked by anonymous 24.02.2018 / 21:26

1 answer

2

The syntax used is a mix of insertion and update, which is wrong, the correct one is:

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

It has some weird things, not recommended in the code or with potential for error, so you can have something else. And you do not save variables in the database, save values, they can be originally stored in the variable.

    
24.02.2018 / 21:40