添加两个计算列的结果

时间:2013-11-02 01:09:44

标签: c# sql ms-access

我需要一些邻近搜索的帮助。我需要在房地产属性数据库上运行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.

1 个答案:

答案 0 :(得分:0)

你在这一行的末尾错过了一个逗号:

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

修正:

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