[英]How do I update a row of data using c# and sql
I'm trying to update an image field where the id is equal to the row selected. 我正在尝试更新id等于所选行的图像字段。
I've tried using the below code 我试过使用下面的代码
String query = "UPDATE ArticlesTBL SET ArticleImg = VALUE (@ArticleImg) WHERE ArticleID = VALUE (@id)";
SqlCommand myCommand = new SqlCommand(query, myConnection);
myCommand.Parameters.AddWithValue("@ArticleImg", "UploadedUserFiles/" + FileUpdate.FileName);
myCommand.Parameters.Add(new SqlParameter("@id", ThisID));
myinfo.Text = query;
But I don't know where to place commas, brackets etc in the query string. 但我不知道在查询字符串中的逗号,括号等位置。 I have a text box outputting my command and it shows that i'm using UPDATE ArticlesTBL SET ArticleImg = @ArticleImg WHERE ArticleID = @id But I'm not actually accessing the parameters.
我有一个文本框输出我的命令,它显示我正在使用UPDATE ArticlesTBL SET ArticleImg = @ArticleImg WHERE ArticleID = @id但我实际上并没有访问参数。 So, how do I access these parameters?
那么,我该如何访问这些参数呢? Thanks
谢谢
The correct syntax for your update statement is 更新语句的正确语法是
String query = @"UPDATE ArticlesTBL
SET ArticleImg = @ArticleImg
WHERE ArticleID = @id";
And to avoid confusion with the constructor of an SqlParameter that accepts a SqlDbType and an Object I suggest to use AddWithValue also for the ID 并且为了避免与接受SqlDbType和Object的SqlParameter的构造函数混淆,我建议也使用AddWithValue作为ID
SqlCommand myCommand = new SqlCommand(query, myConnection);
myCommand.Parameters.AddWithValue("@ArticleImg", "UploadedUserFiles/" + FileUpdate.FileName);
myCommand.Parameters.AddWithValue("@id", ThisID);
myCommand.ExecuteNonQuery();
Of course, the command needs to be executed after you have prepared the command text, added its parameters and associated the connection 当然,在准备好命令文本,添加其参数并关联连接后,需要执行该命令
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.