簡體   English   中英

“在FROM子句中出現語法錯誤”在C#中使用OleDb但在Access本身中沒有相同查詢的錯誤

[英]“Syntax error in FROM clause” using OleDb in C# but no error for same query in Access itself

在c#/ OleDbCommand.ExecuteReader中使用以下SQL語句時,我在FROM子句中遇到語法錯誤。 在MS Access中使用完全相同的語句可以正常工作。

SELECT 
s.idShots, s.shotdata, c.[original], s.[hash], comp.idCompetitions, comp.competitionsname, sh.idShooters, sh.firstname, sh.lastname 
FROM (([Shots] s 
INNER JOIN [ShotsCertificate] c ON c.[uuid] = s.[uuid]) 
INNER JOIN [Competitions] comp ON comp.idCompetitions = s.fidCompetitions) 
INNER JOIN [Shooters] sh ON sh.idShooters = s.fidShooters ORDER BY s.idShots ASC

在c#中:

        OleDbCommand cmd2 = new OleDbCommand("", dbc);
        cmd2.CommandText = "SELECT s.idShots, s.shotdata, c.[original], s.[hash], comp.idCompetitions, comp.competitionsname, sh.idShooters, sh.firstname, sh.lastname FROM" +
            " (([Shots] s" +
            " INNER JOIN [ShotsCertificate] c ON c.[uuid] = s.[uuid])" +
            " INNER JOIN [Competitions] comp ON comp.idCompetitions = s.fidCompetitions)" +
            " INNER JOIN [Shooters] sh ON sh.idShooters = s.fidShooters" +
            " ORDER BY s.idShots ASC";

        log.Debug(cmd2.CommandText);
        OleDbDataReader r = cmd2.ExecuteReader();

dbc連接工作正常,它在以前的一些命令中使用,一切正常。

謝謝你的建議!

我搞定了......沒有評論:/

SELECT
 [Shots].[idShots], [Shots].[shotdata], [ShotsCertificate].[original], [Shots].[hash], [Competitions].[idCompetitions], [Competitions].[competitionsname], [Shooters].[idShooters], [Shooters].[firstname], [Shooters].[lastname] 
FROM (([Shots] 
   INNER JOIN [ShotsCertificate] ON [ShotsCertificate].[uuid] = [Shots].[uuid]) 
   INNER JOIN [Competitions] ON [Competitions].[idCompetitions] = [Shots].[fidCompetitions]) 
   INNER JOIN [Shooters] ON [Shooters].[idShooters] = [Shots].[fidShooters]
ORDER BY [Shots].[idShots] ASC

對於記錄,問題是COMP包含在Access SQL保留字列表中,可能是作為Access DDL的COMPRESSION的縮寫。 將表別名從comp更改為cmpt允許查詢在System.Data.OleDb下成功運行:

sql = "SELECT s.idShots, s.shotdata, c.[original], s.[hash], cmpt.idCompetitions, cmpt.competitionsname, sh.idShooters, sh.firstname, sh.lastname FROM" +
    " (([Shots] s" +
    " INNER JOIN [ShotsCertificate] c ON c.[uuid] = s.[uuid])" +
    " INNER JOIN [Competitions] cmpt ON cmpt.idCompetitions = s.fidCompetitions)" +
    " INNER JOIN [Shooters] sh ON sh.idShooters = s.fidShooters" +
    " ORDER BY s.idShots ASC";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM