首页 > 解决方案 > HttpClient 在 Angular 中提出问题

问题描述

我正在使用 Web api 开发一个 Angular 应用程序。

我创建了一个服务 (sellerService),我可以在其中更新我的数据库中的一些数据HttpClient put

以上工作,但它更新了我的表的所有数据,如下所示;

在我更新我的卖家之前:

前

在我更新我的卖家后:

后

我的卖家服务代码:

  updateSeller(user: string, nbsales: number, pVote: number, nVote: number, idUser: number): Observable<any> {
      return this.http.put('http://localhost:50867/api/seller_user/', {
        'username': user,
        'nbSales': nbsales,
        'positiveVote': pVote,
        'negativeVote': nVote,
        'idUser': idUser
      });
    }

我的更新查询(DAO (c#)):

public static readonly string UPDATE = "update " + TABLE_NAME + " set "
            + COLUMN_USERNAME + " =@username"
            + ", " + COLUMN_NB_SALES + "=@nbSales"
            + ", " + COLUMN_POSITIVE_VOTE + "=@positiveVote"
            + ", " + COLUMN_NEGATIVE_VOTE + " =@negativeVote"
            + ", " + COLUMN_ID_USER + "=@idUser";

        //Update a seller_user
        public static bool Update(Seller_user todo)
        {
            bool state = false;
            using (SqlConnection connection = DataBase.GetConnection())
            {
                connection.Open();
                SqlCommand command = new SqlCommand(UPDATE, connection);

                //command.Parameters.AddWithValue("@idSeller", todo.idSeller);
                command.Parameters.AddWithValue("@username", todo.username);
                command.Parameters.AddWithValue("@nbSales", todo.nbSales);
                command.Parameters.AddWithValue("@positiveVote", todo.positiveVote);
                command.Parameters.AddWithValue("@negativeVote", todo.negativeVote);
                command.Parameters.AddWithValue("@idUser", todo.idUser);

                state = command.ExecuteNonQuery() != 0;
            }
            return state;
        }

提前致谢 ;)

标签: c#angularhttpclientput

解决方案


您错过了 SQL 查询中的where子句。因此它将更新所有记录。

public static readonly string UPDATE = "update " + TABLE_NAME + " set "
            + COLUMN_USERNAME + " =@username"
            + ", " + COLUMN_NB_SALES + "=@nbSales"
            + ", " + COLUMN_POSITIVE_VOTE + "=@positiveVote"
            + ", " + COLUMN_NEGATIVE_VOTE + " =@negativeVote"
            + ", " + COLUMN_ID_USER + "=@idUser"
            + "WHERE "  + COLUMN_ID_USER + "=" + "= @idUser";

推荐阅读