【问题标题】:Add the results of two calculated columns添加两个计算列的结果
【发布时间】:2013-11-02 01:09:44
【问题描述】:

我需要一些关于邻近搜索的帮助。我需要在房地产数据库上运行一个 select 语句,以确定最接近搜索条件的顶部行(用户输入的数字)。然后我需要在 gridview 控件中显示这些值。

我想不通的是如何添加多个计算列的值以创建具有邻近结果的最后一列。

谁能帮我弄清楚如何将多个计算列汇总为一列,然后按此列分组?

以下是我尝试过的代码,但出现错误异常。

aQueryString = "SELECT Top " + txtNumRows.Text;
aQueryString += "propno, ";

if (txtMaxAsk.Text != "")
    aQueryString += "askprice, ";

aQueryString += "sqr(((askprice - " + txtMaxAsk.Text;
aQueryString += ")/1000000)^2) AS m1,";

if (txtMinSqFt.Text != "")
    aQueryString += "sqft, ";

aQueryString += "sqr(((sqft - " + txtMinSqFt.Text;
aQueryString += ")/6000)^2) AS m2 ";
aQueryString += "sum(m1) + sum(m2) FROM property ORDER BY 4 ";

anAccessCommand = anAccessConnection.CreateCommand();
anAccessCommand.CommandText = aQueryString;
gvProperty.DataSource = anAccessCommand.CommandText;
anAccessReader = anAccessCommand.ExecuteReader();
gvProperty.DataSource = anAccessReader;
gvProperty.Visible = true;

try
{
    gvProperty.DataBind();
}
catch (Exception ex)
{
    Response.Write("Binding exception in Button1_Click<BR>");
    Response.Write("Access Exception Handler: {0} " + ex.ToString());
}

这是我得到的 OleDbException:

The SELECT statement includes a reserved word or an argument name that is misspelled or missing, or the punctuation is incorrect.

【问题讨论】:

  • aQueryString += ")/6000)^2) AS m2 应该是 aQueryString += ")/6000)^2) AS m2,猜测。对 aQueryString 进行调试。哦,你对 sql 注入攻击持开放态度..

标签: c# sql ms-access


【解决方案1】:

您在此行末尾缺少逗号:

aQueryString += ")/6000)^2) AS m2 ";

更正:

aQueryString += ")/6000)^2) AS m2, ";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-22
    • 1970-01-01
    • 1970-01-01
    • 2016-04-04
    • 2021-01-30
    • 1970-01-01
    • 1970-01-01
    • 2021-11-13
    相关资源
    最近更新 更多