问题描述
我正在尝试使用 OleDbCommand 向 Access 数据库 (.accdb) 进行 SQL 查询.
I'm trying to make an SQL query with an OleDbCommand into an Access database (.accdb).
虽然这个命令工作正常(在 OleDbCommand.ExecuteReader()
中):
While this command works fine (in a OleDbCommand.ExecuteReader()
):
string command =
"SELECT cred.* " +
"FROM TB_CREDENTIALS cred " +
"INNER JOIN TB_REL_USERS_CREDENTIALS rel ON cred.CRED_ID = rel.REL_USR_CRED_CRED_ID ";
另一个没有,我不明白为什么(我看到的所有示例都使用完全相同的语法):
This other doesn't, and I can't understand why (all examples I see around use the exact same syntax):
string command =
"SELECT cred.* " +
"FROM TB_CREDENTIALS cred " +
"INNER JOIN TB_REL_USERS_CREDENTIALS rel ON cred.CRED_ID = rel.REL_USR_CRED_CRED_ID " +
"INNER JOIN TB_USERS usr ON usr.USR_ID = rel.REL_USR_CRED_USER_ID ";
给出的异常如下System.Data.OleDb.OleDbException
:
查询表达式中的语法错误(缺少运算符)'cred.CRED_ID = rel.REL_USR_CRED_CRED_ID INNER JOIN TB_USERS usr ON usr.USR_ID = rel.REL_USR_CRED_USER_I' (消息在此处剪切)
版本和详细信息:
- 数据库是在 Access 2010 上创建的
.accdb
文件 - 连接是在 C# 中使用
System.Data.OleDb.OleDbConnection
创建的 - 连接提供程序是
Microsoft.ACE.OLEDB.12.0"
(这似乎是一个无用的查询,但我当然会添加一个 WHERE usr.SOME_FIELD = some_condition
)
(This seems like a useless query, but of course I'll add a WHERE usr.SOME_FIELD = some_condition
)
推荐答案
"对于多表连接,您必须将额外的连接嵌套在括号中:"
"For multi-table joins, you have to nest the extra joins in brackets:"
这篇关于OleDb SQL 查询中的两个内部联接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!