删除查询获取字符串的值

时间:2013-05-10 14:26:38

标签: c# sql sql-server ado.net delete-row

我必须从数据库中删除一行。条件是字符串等于字段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的类型是字符串。我确信有一个类似字符串的值,但总是删除任何行!!

  1. 错误在哪里?
  2. 我该如何解决?

2 个答案:

答案 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
  }