Q:

C# updating entries and passing columns in an SQL database

I need to make my code for where the data is inserted into 'Class_1' there are actually 3 score columns (Score_1, Score_2, Score_3) as the application I am building requires the database to keep a record of the last three entered scores. The problem with the code is that every time the data is added it will create a new entry and so what I need the program to do is check to see if the First_Name and Last_Name are present in another entry and if so, update that entry, then what I would like to do is check to see if score_1 has a value present and of course if this is true, skip score_1 and input the data into score_2 but I am completely new to C# so any help here would be much appreciated!, Thankyou!
if(inpClassNumber.Text == "Class 1")
{
    con = new SqlConnection(@"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename = C:\Program Files (x86)\Flux Infusion\Projects\Primary School Quiz System\Primary School Quiz System\Scores.mdf; Integrated Security = True");
    con.Open();
    cmd = new SqlCommand("INSERT INTO Class_1 (First_Name,Last_Name,Score_1) VALUES (@First_Name,@Last_Name,@Score)", con);
    cmd.Parameters.AddWithValue("@First_Name", txtFirstName.Text);
    cmd.Parameters.AddWithValue("@Last_Name", txtLastName.Text);
    cmd.Parameters.AddWithValue("@Score", lblScore.Text);
    cmd.ExecuteNonQuery();
}

A:

If you are running SQL Server 2008 or newer you can use a MERGE TSQL:
MERGE Class_1 AS target
    USING (SELECT @First_Name, @Last_Name, @Score) AS source (fName, lName, score)
    ON (target.First_Name = source.fName and target.Last_Name = source.lName)
    WHEN MATCHED THEN 
        UPDATE SET 
            Score_1 = @Score,
            Score_2 = Score_1,
            Score_3 = Score_2

WHEN NOT MATCHED THEN
    INSERT (First_Name, Last_Name, Score_1)
    VALUES (@First_Name, @Last_Name, @Score);

.
The above code keeps the most recent score in column Score_1, the second most recent in Score_2, and so on.
Here is the link to the TSQL documentation.

