参数中的模型一直具有DAL方法的值,在该方法中查询是DAL方法,但是数据库中的某种方式没有变化。有人知道为什么吗?也没有发生错误。
Controller
:
public ActionResult AddCar(CarModel car)
{
if(ModelState.IsValid)
{
u.AddCar(new DTO.CarDTO(car.Color, car.Type, car.NumberOfSeats, car.Price, car.MaxKilometres, car.Addition, car.ModelID, car.License_Plate, car.Fueltype, car.ReservationStatus));
return RedirectToAction("Index", "Home");
}
return View();
}
Logic
:
public void AddCar(CarDTO c)
{
carDAL.AddCar(new CarDTO(c.Color, c.Type, c.NumberOfSeats, c.Price, c.MaxKilometres, c.Addition, c.ModelID, c.License_Plate, c.Fueltype, c.ReservationStatus));
}
Interface layer
:
public interface ICarDAL<CarDTO>
{
public void AddCar(CarDTO c) { }
}
DAL
,类确实具有连接字符串:
public void AddCar(CarDTO c)
{
using (SqlConnection con = new SqlConnection(connectionString))
{
string query = "INSERT INTO [dbo].[Car](Price, Addition, License_Plate, MaxKm, Type, Color, NumberOfSeats, Fueltype, reservationStatus, ModelID) " +
"VALUES(@Price, @Addition, '@LicensePlate', @MaxKm, '@Type', '@Color', @NumberOfSeats, @FuelType, @Reserved, @ModelID)";
using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.Parameters.AddWithValue("@Price", c.Price);
cmd.Parameters.AddWithValue("@Addition", c.Addition);
cmd.Parameters.AddWithValue("@LicensePlate", c.License_Plate);
cmd.Parameters.AddWithValue("@MaxKm", c.MaxKilometres);
cmd.Parameters.AddWithValue("@Type", c.Type);
cmd.Parameters.AddWithValue("@Color", c.Color);
cmd.Parameters.AddWithValue("@NumberOfSeats", c.NumberOfSeats);
cmd.Parameters.AddWithValue("@FuelType", c.Fueltype);
cmd.Parameters.AddWithValue("@ReservationStatus", c.ReservationStatus);
cmd.Parameters.AddWithValue("@ModelID", c.ModelID);
}
}
}
发现了错误,但我仍然不知道如何解决: https://imgur.com/xjKIqED
解决方案:首先,我需要使用ExecuteNonQuery()
来使查询实际执行某项操作……我认为SqlCommand
已经为您执行了它。其次,我的密码由************组成,我以为这没关系,但我想是这样。
答案 0 :(得分:1)
public void AddCar(CarDTO c)
{
using (SqlConnection con = new SqlConnection(connectionString))
{
if (con.State==ConnectionState.Closed)
{
con.Open();
}
[Your code]
con.Close();
}
}
答案 1 :(得分:-2)
您可能需要使用“保存”功能,在您的代码中没有实际执行SQL命令的地方,没有看到它正在构建,但是过去我曾使用过类似的命令cmd.Execute()