我正在使用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;
}
预先感谢;)
答案 0 :(得分:1)
您错过了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";