-3

您好,我在 C# 中的这个查询上有一个语法错误,我不知道是什么原因造成的,当我在 Microsoft SQL Server 2012 中查询它时,我只是按预期获取数据,谁能帮助我:

        SqlConnection connectie = new SqlConnection("user id=;" +
           "password=;server=;" +
           "Trusted_Connection=false;" +
           "database=; ");

        SqlCommand statsDocenten = new SqlCommand("SELECT enquete_vraag as vraag,AVG(enquete_antwoord_radio) as gemiddelde_antwoord,docent,vak" +
        "FROM ENQUETE_ANTWOORD " +
        "LEFT JOIN KDV " +
        "ON ENQUETE_ANTWOORD.kdv_ID = KDV.kdv_ID " +
        "LEFT JOIN DOCENT " +
        "ON KDV.docent_ID = DOCENT.docent_ID " +
        "LEFT JOIN VAK " +
        "ON KDV.vak_ID = VAK.vak_ID " +
        "LEFT JOIN ENQUETE_VRAAG " +
        "ON ENQUETE_ANTWOORD.enquete_vraag_ID = ENQUETE_VRAAG.enquete_vraag_ID " +
        "WHERE DOCENT.docent_ID = " + docentid + " AND VAK.vak = " + vak + " AND ENQUETE_ANTWOORD.enquete_antwoord_radio != '' " +
        "GROUP BY enquete_vraag,enquete_antwoord_radio,docent,vak", connectie);

我已经在“可能已经有你答案的问题”区域检查了一堆问题,但我不知道哪个问题和答案会影响我的查询。

4

2 回答 2

3

您在vakand之间的第一行缺少一个空格FROM

您绝对应该参数化您的查询(搜索它,您会找到“如何”和“为什么”)。此外,在字符串之前使用 @ 符号(称为逐字字符串)会有更好的运气,并且您可以拥有多行字符串而无需连接一堆字符串:

string sql = @"
SELECT enquete_vraag as vraag,AVG(enquete_antwoord_radio) as gemiddelde_antwoord,docent,vak
FROM ENQUETE_ANTWOORD
LEFT JOIN KDV ...etc...";
于 2013-11-04T16:00:54.617 回答
1
"...as gemiddelde_antwoord,docent,vak" + <----- you need a space here
    "FROM ENQUETE_ANTWOORD " +
...

否则这些行读作

as gemiddelde_antwoord,docent,vakFROM ENQUETE_ANTWOORD
于 2013-11-04T16:02:38.677 回答