0

我必须从数据库中删除一行。条件是当字符串等于字段 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为文本,参数类型Ur为字符串。我确定有一个像字符串这样的值,但总是删除任何行!

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

2 回答 2

3

正如所评论的,您缺少FROM删除语句中的子句。检查 BOL 上的 DELETE sintax。基本的语法是:

DELETE FROM [Tablename] WHERE [Fieldname] LIKE @FieldValue

所以应该是:

DELETE FROM Piece WHERE Url = @ur
于 2013-05-10T14:36:30.833 回答
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
  }
于 2013-05-10T14:33:58.053 回答