0

为什么我会收到错误消息Must declare the scalar variable "@ChargeRate"

public static void InsertSqlSerwer2(BatteryFullInfo info)
{

        SqlConnection MSDEconn;
        MSDEconn = new SqlConnection();
        MSDEconn.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\HOME\Documents\NewDataBase.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";

        MSDEconn.Open();

        string komenda = "INSERT into Battery_Information VALUES  (@ChargeRate, @Id, @DischargeRate, @RemainingCapacity, @Voltage, @BatteryLifePercent, @ChargeStatus)";

        SqlCommand MSDEcommand = new SqlCommand(komenda, MSDEconn);


        MSDEcommand.Parameters.AddWithValue("@ChargeRate", info.ChargeRate);
        MSDEcommand.Parameters.AddWithValue("@Id", info.IdBateria);
        MSDEcommand.Parameters.AddWithValue("@DischargeRate", info.DischargeRate);
        MSDEcommand.Parameters.AddWithValue("@RemainingCapacity", info.RemainingCapacity);
        MSDEcommand.Parameters.AddWithValue("@Voltage", info.Voltage);
        MSDEcommand.Parameters.AddWithValue("@BatteryLifePercent", info.BatteryLifePercent);
        MSDEcommand.Parameters.AddWithValue("@ChargeStatus", info.ChargeStatus);


        MSDEcommand.Parameters.Clear();

        MSDEcommand.ExecuteNonQuery();
        MSDEconn.Close();

}
4

1 回答 1

0

因为您正在清除参数。你经历了所有的努力AddWithValue,然后你执行MSDEcommand.Parameters.Clear(),这就摆脱了它们。删除那条线,我认为这会起作用。

于 2013-07-03T15:34:46.463 回答