标量变量未声明

时间:2012-09-11 18:23:08

标签: c# asp.net sql

protected void DropDownList8_SelectedIndexChanged(object sender, EventArgs e)
{
    var connectionString = (ConfigurationManager.ConnectionStrings["Connection"].ConnectionString);
    var updateCmd = "UPDATE [CarTab] SET Rent= 1 WHERE ([Model] = @Model)";
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        SqlCommand command = new SqlCommand(updateCmd, connection);
        command.Connection.Open();
        command.ExecuteNonQuery();
    }
}

错误:必须为@Model声明标量变量。 我该删除/添加什么?无法弄清楚。 提前谢谢。

2 个答案:

答案 0 :(得分:2)

您可以尝试使用

 command.Parameters.AddWithValue("@Model", value);

您完成代码

protected void DropDownList8_SelectedIndexChanged(object sender, EventArgs e)
{
    var connectionString = (ConfigurationManager.ConnectionStrings["Connection"].ConnectionString);
    var updateCmd = "UPDATE [CarTab] SET Rent= 1 WHERE ([Model] = @Model)";
    using (SqlConnection connection = new SqlConnection(
               connectionString))
    {
        using(var command = new SqlCommand(updateCmd, connection))
        {
          command.Parameters.AddWithValue("@Model", value); //Replace with your value

          command.Connection.Open();
          command.ExecuteNonQuery();
        }
    }
}

答案 1 :(得分:0)

您必须添加参数@Model,例如:

    updateCmd.Parameters.Add("@Model", SqlDbType.SomeType);
    updateCmd.Parameters["@Model"].Value = something;