HttpClient 将问题放在 Angular
HttpClient put issue in Angular
我正在使用 Web api 开发 Angular 应用程序。
我创建了一个服务 (sellerService),我可以在其中使用 HttpClient put
.
更新数据库中的一些数据
以上有效,但它更新了我的 table 的所有数据,如下所示;
在我更新卖家之前:
更新卖家后:
我的卖家服务码:
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;
}
提前致谢;)
您错过了 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";
我正在使用 Web api 开发 Angular 应用程序。
我创建了一个服务 (sellerService),我可以在其中使用 HttpClient put
.
以上有效,但它更新了我的 table 的所有数据,如下所示;
在我更新卖家之前:
更新卖家后:
我的卖家服务码:
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;
}
提前致谢;)
您错过了 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";