Skip to content
Advertisement

Update SQL table using C#

I’m trying to update EpisodeId no:117 in the Episode table and it executes successfully but when I check the table it is not updated. int episode Id = 117;

int seriesNumber = 9;
int episodeNumber = 13;
string episodeType = "abnormal episode";
string title = "Reconsideration";
string notes = "recuring behaviour";

//connectionString
 
string connectionString = "data source=LAPTOP-VLO4EFFQ\MSSQLSERVER01; database=DoctorWho; integrated Security=True;";

//connection using
using (SqlConnection conn = new SqlConnection(connectionString))
{
    
    conn.Open();
    Console.WriteLine("Connection sucessfull");

    string query = "UPDATE tblEpisode " +
        "(SeriesNumber, EpisodeNumber, EpisodeType, Title, Notes)" +
        "(SET SeriesNumber=@SeriesNumber, EpisodeNumber=@EpisodeNumber, EpisodeType=@EpisodeType, Title=@Title, Notes=@Notes)" +
        "(WHERE EpisodeId=@EpisodeId)";

    using (SqlCommand command = new SqlCommand(query, conn))
    {
        //updating data in the sql table with the initial variables  
        command.Parameters.Add("@EpisodeId", System.Data.SqlDbType.Int).Value = episodeId;
        command.Parameters.Add("@SeriesNumber", System.Data.SqlDbType.Int).Value = seriesNumber;
        command.Parameters.Add("@EpisodeNumber", System.Data.SqlDbType.Int).Value = episodeNumber;
        command.Parameters.Add("@EpisodeType", System.Data.SqlDbType.NVarChar).Value = episodeType;
        command.Parameters.Add("@Title", System.Data.SqlDbType.NVarChar).Value = title;
        command.Parameters.Add("@Notes", System.Data.SqlDbType.NVarChar).Value = notes;

    }


    conn.Close();
    Console.WriteLine("connection is closed!!");
}

Advertisement

Answer

There are some issues with your SQL update statement.Look at following for reference to Update Statement in SQL LINK

There is also an easier way to add the parameters using the AddWithValue Method. LINK

Next, you are not executing the SQL command. For Update statements use the ExecuteNonQuery() method. LINK

Also as @Nikki9696 mentioned, episodeId is not declared. Make sure to declare episodeId with your other variables.

int episodeId = 117;
int seriesNumber = 9;
int episodeNumber = 13;
string episodeType = "abnormal episode";
string title = "Reconsideration";
string notes = "recuring behaviour";

//connectionString

string connectionString = "data source=LAPTOP-VLO4EFFQ\MSSQLSERVER01; database=DoctorWho; integrated Security=True;";

//connection using
using (SqlConnection conn = new SqlConnection(connectionString))
{

conn.Open();
Console.WriteLine("Connection sucessfull");

string query = "UPDATE tblEpisode " +        
    "SET SeriesNumber=@SeriesNumber, EpisodeNumber=@EpisodeNumber, EpisodeType=@EpisodeType, Title=@Title, Notes=@Notes " +
    " WHERE EpisodeId=@EpisodeId;";

using (SqlCommand command = new SqlCommand(query, conn))
{
    //updating data in the sql table with the initial variables  
    command.Parameters.AddWithValue("@EpisodeId", episodeId);
    command.Parameters.AddWithValue("@SeriesNumber", seriesNumber);
    command.Parameters.AddWithValue("@EpisodeNumber", episodeNumber);
    command.Parameters.AddWithValue("@EpisodeType", episodeType);
    command.Parameters.AddWithValue("@Title", title);
    command.Parameters.AddWithValue("@Notes", notes);
    
    command.ExecuteNonQuery();

}


conn.Close();
Console.WriteLine("connection is closed!!");
}
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement