Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am using MS SQL server and struggling to get an UPDATE query to work. IS UPDATE the correct SQL query for MS Server? I have googled it but seemed to have little joy.

my query is:

SqlCommand cmd = new SqlCommand("UPDATE [User] SET Age='age', Height='height' Weight='weight', Target_Weight='target'", conn)


I thought this was the correct syntax, I am obviously wrong! Could someone have a quick look for me and tell me where I am going wrong!

Dan
Posted
Comments
DanHodgson88 2-Aug-11 10:49am    
Simple error wasn't acctually my SQL that was wrong, I had an if statment that could not be executed due to another variable being wrong. I have now changed the query to use parameters though I has total forgot to put them in! Thansk for the replies guys

First of all wants to know what error you are getting?

SQL
SqlCommand cmd = new SqlCommand("UPDATE [User] SET Age='age', Height='height', Weight='weight', Target_Weight='target'", conn)


Actually you are missing a comma after Height and Weight.

Use where clause else every row will be updated.
 
Share this answer
 
v2
Comments
RaisKazi 2-Aug-11 11:40am    
5!
use sqlparameters for age, height and weight.
use where clause to specify for which user the update applies, otherwise all users have same age, height and weight.
 
Share this answer
 
Comments
RaisKazi 2-Aug-11 11:40am    
5!
I don't see any problem with the syntax, but what values are you passing to the statement? Age='age', Height='height' Weight='weight', Target_Weight='target' doesn't make sense. Use a parameterized query and don't forget to use a WHERE clause, otherwise, your entire table will be updated. The general syntax is:
SQL
UPDATE Table1
SET Col1 = @Col1,
    Col2 = @Col2,
    Col3 = @Col3
WHERE (Col4 = @Col4)
 
Share this answer
 
Comments
RaisKazi 2-Aug-11 11:40am    
5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900