"Syntax error in FROM clause" 在 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 Reserved Words 的列表中,大概是 Access DDL 的 COMPRESSION
的缩写。将 table 别名从 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";
在 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 Reserved Words 的列表中,大概是 Access DDL 的 COMPRESSION
的缩写。将 table 别名从 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";