为什么我会收到错误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();
}
答案 0 :(得分:0)
因为您正在清除参数。你完成所有努力到AddWithValue
,然后你执行MSDEcommand.Parameters.Clear()
,它们全部清除它们。删除该行,我认为这将有效。