Dougiee
Dougiee

Reputation: 19

Updating 2 rows in SQL database with one query string "UPDATE"

I need to figure out how to update the two rows in my database but cnt get my query string correct..

Here is the code that I am using. If you could provide me with a query string that would be great..

string birdnametextupdate = birdsnametext.Text;
string birdnamedetailsupdate = birdnamedetails.Text;

int row = int.Parse(Request.QueryString["PhotoID"]);

string query = "UPDATE Photos Set PhotoName = @PhotoNewName Set Deatils = @DetailsNew WHERE PhotoID = @PhotoID";

SqlCommand myCommand = new SqlCommand(query, myConnection);

//create parameterised object
myCommand.Parameters.AddWithValue("@PhotoNewName", birdnametextupdate);
myCommand.Parameters.AddWithValue("@DetailsNew", birdnamedetailsupdate);

myCommand.Parameters.AddWithValue("@PhotoID", row);

myCommand.ExecuteNonQuery();

Upvotes: 0

Views: 830

Answers (2)

try this

 string query = "UPDATE Photos Set PhotoName = '@PhotoNewName' ,Details  = '@DetailsNew' WHERE PhotoID = '@PhotoID'";

I saw error in your query, do not duplicate set. just use comma instead

Upvotes: 1

Kyro
Kyro

Reputation: 768

int photoId = int.Parse(Request.QueryString["PhotoID"]);
string query = "UPDATE Photos SET PhotoName = @PhotoNewName, Details = @DetailsNew WHERE PhotoID = @PhotoID";

using(var cmd = new SqlCommand(query, myConnection))
{
    cmd.Parameters.Add("@PhotoNewName").Value = birdsnametext.Text;
    cmd.Parameters.Add("@DetailsNew").Value = birdnamedetails.Text;
    cmd.Parameters.Add("@PhotoID").Value = photoId;
    cmd.ExecuteNonQuery();
}

The Sql syntax is: https://msdn.microsoft.com/en-us/library/ms177523.aspx

A simple example is:

UPDATE <tableName>
SET
    <Column1> = <newColumn1Value>
   ,<Column2> = <newColumn2Value>
WHERE <condition>

Upvotes: 1

Related Questions