【问题标题】:Scalar variable not declared未声明标量变量
【发布时间】:2012-09-12 02:52:01
【问题描述】:
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 声明标量变量。 我应该在那里删除/添加什么?想不通。 提前致谢。

【问题讨论】:

    标签: c# asp.net sql


    【解决方案1】:

    你可以试试

     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();
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      必须添加参数@Model,如:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-04
        • 2018-03-07
        • 1970-01-01
        • 2011-11-03
        • 1970-01-01
        • 2013-07-03
        相关资源
        最近更新 更多