I am using MySqlCommand in MySqlConnection in a WinForms C# application to insert data into my database.
NOTE: lets say ALL names are unique, no matter what!!
how can I update a row if the column value in "name" already exists in database?
and if it does not exist, i want to insert new row.
Here is my code now:
command.CommandText = @"
INSERT INTO users (name, age, job)
VALUES (@name, @age, @job)
ON DUPLICATE KEY UPDATE users SET age = @age, job = @job
";
I already set values to @name, @age and @job like this:
command.Parameters.AddWithValue("@name", Users.Name);
I want to check, if @name already exists in the "name" column in the database. So if @name is "John", it should check if "John" already exists.
And if exists, I only want to update the age and job on that row.
And if "John" does not exist, insert the full row with name+age+job.
This code gives me an error like this:
An exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in mscorlib.dll but was not handled in user code
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ON DUPLICATE KEY UPDATE users SET age = 50, job =' at line 1
Aucun commentaire:
Enregistrer un commentaire