【问题标题】:Please detect the error in my query, I am getting an "Invalid column name" error.请检测我的查询中的错误,我收到“无效的列名”错误。
【发布时间】:2014-01-28 10:51:40
【问题描述】:
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("Data Source=HKGROX-PC\\SQLEXPRESS;Initial Catalog=example;Integrated Security=True");
        con.Open();
        string value = DropDownList1.SelectedValue.ToString();

        switch (value)
        {
            case "BJP":
                string s = "select BJP from noofvotes where year= "+ DropDownList2.Text+" and const=" + DropDownList3.SelectedValue + "";
                SqlCommand cmd = new SqlCommand(s,con);
                DataSet ds = new DataSet();
                SqlDataAdapter sda = new SqlDataAdapter(cmd);
                sda.Fill(ds);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    DataRow dr = ds.Tables[0].Rows[0];
                    TextBox1.Text = dr["BJP"].ToString();
                }
                break;

        }
        con.Close();
    }

在这里我得到无效的列名“Rohini”。我有一个将值提供给列表框的表格。

【问题讨论】:

  • YEAR 是保留关键字。你的表的真实列名是什么?
  • 您在哪里看到此错误?这段代码中没有提到Rohini
  • 我想DropDownList3.SelectedValue 包含值“Rohini”,它没有被'' 包围,所以它被解释为一个列(where const=Rohini)。按照@Steve 的建议使用参数。

标签: c#


【解决方案1】:

假设您有真正命名为 YEAR 和 CONST 的列,我会添加,以避免混淆错误

string s = "select BJP from noofvotes where [year]= @yval and [const]=@cval";
SqlCommand cmd = new SqlCommand(s,con);
cmd.Parameters.AddWithValue("@yval",DropDownList2.Text);
cmd.Parameters.AddWithValue("@cval",DropDownList3.SelectedValue);
....

将保留字 YEAR 括在方括号中将允许将该名称用作列标识符(虽然这确实是一种不好的做法)。 CONST 字没有保留,但由于众所周知的语言关键字,我发现它也令人困惑。

上述查询中的另一个问题(肯定是第一个错误的来源)是 DrowpDownList3 的SelectedValue 属性。它被插入到查询中而不用单引号引起来。如果我想象它包含单词Rohini,那么您的原始文本会变成这样

string s = "select BJP from noofvotes where year=2014 and const=Rohini";

并且,通过这种方式,该值被视为列名

同样,将其作为查询的参数应该可以解决问题。

【讨论】:

  • const 不是关键字。但是你也可以在方括号中使用它。
  • @user1517636 你能把这里的编辑解释为评论吗?
【解决方案2】:

试试这个

"select BJP from noofvotes where [year] = "+ DropDownList2.Text+" and const=" + DropDownList3.SelectedValue + "";

这里是Sql Server 的保留Keywords 列表 http://technet.microsoft.com/en-us/library/aa238507%28v=sql.80%29.aspx

【讨论】:

    【解决方案3】:

    如果您想快速修复,请替换:

    string s = "select BJP from noofvotes where year= "+ DropDownList2.Text+" and const=" + DropDownList3.SelectedValue + "";
    

    有了这个:

    string s = "select BJP from noofvotes where [year]= '"+ DropDownList2.Text+"' and const='" + DropDownList3.SelectedValue + "'";
    

    如果您想要更安全的修复,请尽可能使用SqlParameters,它可以让黑客远离您的数据库。

    string s = "SELECT BJP FROM noofvotes WHERE [year]= @year AND const=@const";
    SqlCommand cmd = new SqlCommand(s, con);
    cmd.Parameters.AddWithValue("@year", DropDownList2.Text);
    cmd.Parameters.AddWithValue("@const", DropDownList3.SelectedValue);
    

    【讨论】:

      【解决方案4】:

      如果您的列(年份和常量)是 Varchar2 类型,则使用单引号。

      string s = "select BJP from noofvotes where year= '"+ DropDownList2.Text+"' and const='" + DropDownList3.SelectedValue + "'";
      SqlCommand cmd = new SqlCommand(s,con);
      

      如果 Columns (year & const) 是数字列,您必须使用如下:

      string s = "select BJP from noofvotes where year= "+ Convert.ToInt32(DropDownList2.Text)+" and const=" + Convert.ToInt32(DropDownList3.SelectedValue) + "";
      SqlCommand cmd = new SqlCommand(s,con);
      

      【讨论】:

        猜你喜欢
        • 2011-12-19
        • 1970-01-01
        • 1970-01-01
        • 2011-06-21
        • 2018-01-18
        • 2017-07-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多