0
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 声明标量变量。我应该在那里删除/添加什么?想不通。提前致谢。

4

2 回答 2

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();
        }
    }
}
于 2012-09-11T18:26:20.320 回答
0

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

    updateCmd.Parameters.Add("@Model", SqlDbType.SomeType);
    updateCmd.Parameters["@Model"].Value = something;
于 2012-09-11T18:25:08.683 回答