MySql查询 - 选择Where,Alias&参数

时间:2011-04-24 09:48:39

标签: c# mysql alias where-clause

我在使用别名和参数的MySQL Select Where语句时遇到了一些麻烦。我的问题在于声明的Where部分。就目前而言,当我尝试使用任何参数时,我没有返回任何结果。

有关陈述是:

SELECT postcode, suburb, streetname, categorycode, DATE_FORMAT(dateRecorded, '%d/%m/%Y') AS Expr1, DATE_FORMAT(dateLastModified, '%d/%m/%Y') AS Expr2, status FROM incidentdetails WHERE (postcode = @postcode) AND (suburb = @suburb) AND (categorycode = @categorycode) AND (status = @status)

使用正确的参数执行此查询将返回正确的列,但不会返回数据。

完全删除where子句我按预期获得了完整的表。

WHERE (postcode = @postcode) AND (suburb = @suburb) AND (categorycode = @categorycode) AND (status = @status)中的where子句更改为WHERE (postcode = 4020)确实有效 - 正如预期的那样。

用(postcode = @postcode)替换WHERE子句并传递参数(如下所示)不起作用。

sqlFillRelated.Parameters.AddWithValue("@postcode", int.Parse(PostcodeTxtBox.Text.ToString()))

从server explorer SQL命令运行时,完整查询(带参数)成功运行。

        string sqlFILL = "SELECT postcode, suburb, streetname, categorycode, DATE_FORMAT(dateRecorded, '%d/%m/%Y') AS Expr1, DATE_FORMAT(dateLastModified, '%d/%m/%Y') AS Expr2, status FROM incidentdetails WHERE (postcode = @postcode) AND (suburb = @suburb) AND (categorycode = @categorycode) AND (status = @status)";
    string sql = "SELECT COUNT(*) FROM incidentdetails WHERE (postcode = @postcode) AND (suburb = @suburb) AND (categorycode = @categorycode) AND (status = @status)";
    MySqlConnection mycon = new MySqlConnection(sqlconnection);
    mycon.Open();
    MySqlCommand selectRelatedCmd = new MySqlCommand(sql, mycon);
    MySqlCommand sqlFillRelated = new MySqlCommand(sqlFILL, mycon);
    int matches = 0;
    selectRelatedCmd.Parameters.AddWithValue("@postcode", int.Parse(PostcodeTxtBox.Text.ToString()));
    selectRelatedCmd.Parameters.AddWithValue("@suburb", SuburbTxtBox.Text.ToString());
    selectRelatedCmd.Parameters.AddWithValue("@categorycode",IncidentTypeDropList.Text.ToString());
    selectRelatedCmd.Parameters.AddWithValue("@status", "Open");
    sqlFillRelated.Parameters.AddWithValue("@postcode", int.Parse(PostcodeTxtBox.Text.ToString()));
    sqlFillRelated.Parameters.AddWithValue("@suburb", SuburbTxtBox.Text.ToString());
    sqlFillRelated.Parameters.AddWithValue("@categorycode", IncidentTypeDropList.Text.ToString());
    sqlFillRelated.Parameters.AddWithValue("@status", "Open");

    matches = int.Parse(selectRelatedCmd.ExecuteScalar().ToString());
    if (matches == 0)
    {
        matchingIncidentPanel.Visible = false;  
    }
    else if (matches >= 1)
    {
        matchingIncidentPanel.Visible = true;
    }

    MySqlDataAdapter da = new MySqlDataAdapter(sqlFILL, mycon);
    DataTable table = new DataTable();
    table.Locale = System.Globalization.CultureInfo.InvariantCulture;
    da.Fill(table);
    g.DataSource = table;
    g.DataBind();

    mycon.Close();

2 个答案:

答案 0 :(得分:1)

好的,这是一个很长的镜头因为我不确定你使用的语言是什么(是C#?)但是在设置参数时使用@会让我感到奇怪。

通常,您使用一些魔术字符来表示SQL查询中的参数,但是当您以编程方式设置它时,您将省略此字符。

你试过吗

sqlFillRelated.Parameters.AddWithValue("status", "Open")

而不是

sqlFillRelated.Parameters.AddWithValue("@status", "Open")

答案 1 :(得分:1)

是C#。将'@'前缀丢弃到C#代码中的参数。