我必须从数据库中删除一行。条件是字符串等于字段url:
public void Delete_Piece(string ur)
{
this.Connect();
using (connexion)
{
using (SqlCommand sqlCmd = new SqlCommand("Delete Piece where Url like @ur", connexion))
{
sqlCmd.Parameters.AddWithValue("@ur", ur);
try
{
sqlCmd.ExecuteNonQuery();
}
catch { }
}
}
}
字段Url
的类型是Text,参数Ur
的类型是字符串。我确信有一个类似字符串的值,但总是删除任何行!!
答案 0 :(得分:3)
如评论所述,您在删除语句中缺少FROM
子句。 Check DELETE sintax on BOL。基本的sintax是:
DELETE FROM [Tablename] WHERE [Fieldname] LIKE @FieldValue
所以它应该是:
DELETE FROM Piece WHERE Url = @ur
答案 1 :(得分:1)
您的SqlCommand
格式不正确。您的查询应该是这样的:
DELETE FROM table_name
WHERE some_column=some_value
在你的情况下:
using (SqlCommand sqlCmd = new SqlCommand("Delete From Piece where
CONVERT(NVARCHAR(MAX), Url) =@ur", connexion))
{
//Your code
}