2017-02-16 112 views
1

当使用C#中的以下SQL语句/ OleDbCommand.ExecuteReader我得到一个语法错误在FROM子句。 在MS Access中使用完全相同的语句直接正常工作。使用C#OleDb的,但在接入号码错误相同的查询“FROM子句语法错误”本身

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的连接工作正常,它在一些以前的命令使用,一切正常。

感谢您的建议!

+0

为[射击],[ShotsCertificate],[赛事]和[射击]所有表,或者是一个或更多他们在Access中保存的选择查询? –

+0

尝试在别名前使用'As'。 – Gustav

+0

都是正常的表,没有观点或东西 – Christopher

回答

0

为了记录在案,问题是,COMP被列入Access SQL Reserved Words名单,想必作为COMPRESSION用于访问DDL的缩写。改变从compcmpt表的别名允许查询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"; 
+0

谢谢!有没有办法获得更精确的错误信息? – Christopher

0

我得到它的工作...没有评论:/

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