c# - 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;
}
提前致谢 ;)
解决方案
您错过了 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";
推荐阅读
- java - Android java调用url
- verilog - 如何使用`include从外部文件导入参数?
- html - Chrome 开发工具 - 在页面周围添加额外的空白
- c++ - 在 C++ 中显示尾随零
- javascript - 如何使用循环获取数组中的工作表数据
- java - 消息监听器和 jmslistener 注解有什么区别
- javascript - PHP Laravel 表单提交问题
- frida - frida - 在 android apk 中挂钩套接字输入流
- broadcast - TRON 区块链:通过公共节点的 API 创建、签署和广播交易
- r - Function two plots in a 1*2 array