[英]Query works in SSMS but not from the C#?
這是一個基本的汽車租賃應用程序,但我對其進行了簡化。 想一想,表中只有兩個變量:
rentID: int
carID: int
當我在 SQL 服務器上執行此操作時,此操作有效:
UPDATE tblCars SET rentID=15 where carID=4
這是 C# 版本。 這一行完全適用於一些基本命令。 但是由於這有很多“和”,所以它不起作用?我在這里做錯了什么?
SqlCommand cmd = new SqlCommand("update tblCars SET rentID= '" + Convert.ToInt32(rentID) +"'where carID='" + Convert.ToInt32(carID) + "'", con);
我建議你參數化你的 SQL 以避免像 SQL 注入這樣的事情:
using (SqlCommand cmd = new SqlCommand("UPDATE tblCars SET rentID=@rentId WHERE carID=@carId;", con))
{
cmd.Parameters.AddWithValue("@rentId", Convert.ToInt32(rentID));
cmd.Parameters.AddWithValue("@carId", Convert.ToInt32(carId));
cmd.ExecuteNonQuery();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.