我使用jquery ajax将值发送到我的.net web服务,然后再将这些值发送到存储过程。我遇到的问题是,无论我向web服务发送什么值,它们都会作为一个字符插入到我的DB中。例如,“测试”变成“t”。
我的所有接收表列的最大值都不是1。使用Firefox的firebug,我已经确认我的ajax方法确实在向web服务发布预期的值。
Web服务:
[WebMethod]
public void InsertAllVehicles(string Make, string Model, string SubModel, decimal Liter, string Cylinder,string Fuel, string FuelDel, string Asp, string EngDesg)
{
using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["localConnectionString"].ConnectionString))
{
using (SqlCommand comm = new SqlCommand())
{
conn.Open();
comm.Connection = conn;
comm.CommandType = CommandType.StoredProcedure;
comm.CommandText = "Vehicle_InsertAll";
if (Make.Length >=1)
comm.Parameters.AddWithValue("@Make", Make);
if (Model.Length >=1)
comm.Parameters.AddWithValue("@Model", Model);
if (SubModel.Length >=1)
comm.Parameters.AddWithValue("@SubModel", SubModel);
if (Liter != 91)
comm.Parameters.AddWithValue("@Liter", Liter);
if (Cylinder.Length >= 1)
comm.Parameters.AddWithValue("@Cylinder", Cylinder);
if (Fuel.Length >= 1)
comm.Parameters.AddWithValue("@Fuel", Fuel);
if (FuelDel.Length >= 1)
comm.Parameters.AddWithValue("@FuelDel", FuelDel);
if (Asp.Length >= 1)
comm.Parameters.AddWithValue("@Asp", Asp);
if (EngDesg.Length != 0)
comm.Parameters.AddWithValue("@EngDesg", EngDesg);
comm.ExecuteNonQuery();
conn.Close();
}
}
}
存储过程
ALTER PROCEDURE dbo.Vehicle_InsertAll
@Make varchar = null,
@Model varchar = null,
@SubModel varchar = null,
@Liter decimal = null,
@Cylinder varchar = null,
@Fuel varchar = null,
@FuelDel varchar = null,
@Asp varchar = null,
@EngDesg varchar = null
AS
IF @Make IS NOT NULL
BEGIN
INSERT INTO VehicleMakes (VehicleMake) VALUES(@Make)
END
IF @Model IS NOT NULL
BEGIN
INSERT INTO VehicleModels (Name) VALUES(@Model)
END
IF @SubModel IS NOT NULL
BEGIN
INSERT INTO VehicleSubModels (Name) VALUES(@SubModel)
END
IF @Liter IS NOT NULL
BEGIN
INSERT INTO VehicleLiters (Liters) VALUES(@Liter)
END
IF @Cylinder IS NOT NULL
BEGIN
INSERT INTO VehicleCylinders (Cylinders) VALUES(@Cylinder)
END
IF @Fuel IS NOT NULL
BEGIN
INSERT INTO VehicleFuel (FuelType) VALUES (@Fuel)
END
IF @FuelDel IS NOT NULL
BEGIN
INSERT INTO VehicleFuelDelivery (Delivery) VALUES (@FuelDel)
END
IF @Asp IS NOT NULL
BEGIN
INSERT INTO VehicleAspiration (Asp) VALUES (@Asp)
END
IF @EngDesg IS NOT NULL
BEGIN
INSERT INTO VehicleEngDesg (Designation) VALUES (@EngDesg)
END
RETURN