0

此 API 删除电话号码。API 应返回true = successfalse = error(电话未被删除)。如果表中不存在电话号码,则返回 false?

我该如何解决?

[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string Delete(string phoneNumber)
    {
        const string sql =
            "DELETE FROM `phones` WHERE  `phone_number`=@phoneNumber LIMIT 1;";
        try
        {
            Connection = new MySqlConnection(GetConnectionString());
            Connection.Open();
            var cmd = new MySqlCommand(sql, Connection);
            cmd.Prepare();
            cmd.Parameters.AddWithValue("@phoneNumber", phoneNumber);
            var resultSet = cmd.ExecuteNonQuery();
            if (!resultSet.Equals(0))
                return JsonConvert.SerializeObject(new Result(true));
        }
        catch (MySqlException err)
        {
            return JsonConvert.SerializeObject(new Result(false));
        }
        finally
        {
            if (Connection != null)
            {
                Connection.Close();
            }
        }
        return JsonConvert.SerializeObject(new Result(false));
    }
4

0 回答 0