开发者

Adding the WHERE Statement in the SQL

Hi guys after the help in another post I managed to get the following Update SQL statement to work however I wish to add a WHERE.

So I have:

cmd = new SqlCommand("UPDATE Schedule SET Schd_Avaliable = '" + "No" + "'", con);
        cmd.ExecuteNonQuery();

And I want to add a Where which looks for the Schd_ID in the table and a schdid which is from a session however with all the punctuation im unsure where to put it.

This is the Where I made:

WHERE Schd_ID = schdid 

just unsure where to put that exactly in the line below without it throwing an error:

cmd = new SqlCommand("UPDATE Schedule S开发者_高级运维ET Schd_Avaliable = '" + "No" + "'", con);
            cmd.ExecuteNonQuery();

Mark


Try this:

 string sql = "UPDATE Schedule SET Schd_Avaliable = 'No' WHERE Schd_ID = @schdid";
 cmd = new SqlCommand(sql, con);
 cmd.Parameters.Add("@schdid", int.Parse(Session["SchdID"].ToString()));
 cmd.ExecuteNonQuery();

Modify as needed for your session, and column names.


It is recommended to use Sql Parameters in this situation.

cmd = new SqlCommand(@"UPDATE Schedule
                          SET Schd_Avaliable = @ScheduleAvailable
                        WHERE Schd_ID = @ScheduleID", con);
cmd.Parameters.Add(new SqlParameter("@ScheduleAvailable", "No") );
cmd.Parameters.Add(new SqlParameter("@ScheduleID", schdid.ToString()));
cmd.ExecuteNonQuery();


cmd = new SqlCommand("UPDATE Schedule SET Schd_Avaliable = '" + "No" + "' WHERE Schd_ID ='" + schdid + "'", con);
cmd.ExecuteNonQuery();


"UPDATE Schedule SET Schd_Avaliable = '" + "No" + "'" + "WHERE Schd_ID = '" + schdid + '"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜