mardi 1 décembre 2015

SQL with C# - UPDATE not working but INSERT does work

I am using SQLite with C# and have a table in a database that I want to insert a row into if the table is empty otherwise update the row in the table if there is a row in there. This table will only ever have one row so it isn't necessary to constrain the UPDATE command. If I comment out the update part and row count part to just leave the insert part then it inserts a row fine but the code below to implement the functionality I want throws an error in Visual Studio and I can't figure out why.

using (SQLiteConnection con = new SQLiteConnection(cs))
    {
        con.Open();

        using (SQLiteCommand cmd = new SQLiteCommand(con))
        {
            cmd.CommandText = "UPDATE details SET (initials = @init, participantID = @ID, affectedSide = @side, age = @age, comments = @comm) IF @@ROWCOUNT = 0 INSERT INTO details (initials, participantID , affectedSide , age, comments) VALUES (@init, @ID, @side, @age, @comm)";

            cmd.Parameters.AddWithValue("@init", InitalsTextBox.Text);
            cmd.Parameters.AddWithValue("@ID", IDTextBox.Text);
            cmd.Parameters.AddWithValue("@side", side);
            cmd.Parameters.AddWithValue("@age", ageindex);
            cmd.Parameters.AddWithValue("@comm", ParticipantText.Text);

            cmd.ExecuteNonQuery();
        }
        con.Close();
    }

Aucun commentaire:

Enregistrer un commentaire