SELECT 语句中的 OleDbCommand 语法错误 0x80040E14
OleDbCommand syntax error 0x80040E14 in SELECT Statement
此查询在 Access 2010 中完美运行:
SELECT te.[EnterpirseName], COUNT(to.[ID]) AS Orgs, SUM(to.[Members]) AS Mems
FROM tbl_Enterprise AS te, tbl_Organizations AS to
WHERE to.[Enterprise_ID] = te.[ID] AND te.[Status] = 1
GROUP BY te.[EnterpirseName]
然而,当我尝试从我的 C#
winforms 应用程序执行它时,出现语法错误:
0x80040E14 Syntax error near keyword FROM
编辑: 我的 C# 代码:
string strCommand = "SELECT te.[EnterpirseName], COUNT(to.[ID]) AS Orgs, SUM(to.[Members]) AS Mems" +
" FROM tbl_Enterprise AS te, tbl_Organizations AS to" +
" WHERE to.[Enterprise_ID] = te.[ID]" +
" AND te.[Status] = 1" +
" GROUP BY te.[EnterpirseName]";
DataTable dt = new DataTable();
using (OleDbConnection conn = new OleDbConnection(strConnString))
{
OleDbDataAdapter da = new OleDbDataAdapter(strCommand, conn);
using (DataSet ds = new DataSet())
{
// THIS IS WHERE I GET THE ERROR
da.Fill(ds);
dt = ds.Tables[0];
}
}
这就是 strCommand
在调试中的样子:
您确定您的 C# 应用程序没有使用其他引擎,例如 SQL 服务器吗?在SQL服务器中,TO是保留字,所以应该用[To]代替。
TO也是Access中的保留字。尝试将其替换为 tbl_Org
.
之类的内容
此查询在 Access 2010 中完美运行:
SELECT te.[EnterpirseName], COUNT(to.[ID]) AS Orgs, SUM(to.[Members]) AS Mems
FROM tbl_Enterprise AS te, tbl_Organizations AS to
WHERE to.[Enterprise_ID] = te.[ID] AND te.[Status] = 1
GROUP BY te.[EnterpirseName]
然而,当我尝试从我的 C#
winforms 应用程序执行它时,出现语法错误:
0x80040E14 Syntax error near keyword FROM
编辑: 我的 C# 代码:
string strCommand = "SELECT te.[EnterpirseName], COUNT(to.[ID]) AS Orgs, SUM(to.[Members]) AS Mems" +
" FROM tbl_Enterprise AS te, tbl_Organizations AS to" +
" WHERE to.[Enterprise_ID] = te.[ID]" +
" AND te.[Status] = 1" +
" GROUP BY te.[EnterpirseName]";
DataTable dt = new DataTable();
using (OleDbConnection conn = new OleDbConnection(strConnString))
{
OleDbDataAdapter da = new OleDbDataAdapter(strCommand, conn);
using (DataSet ds = new DataSet())
{
// THIS IS WHERE I GET THE ERROR
da.Fill(ds);
dt = ds.Tables[0];
}
}
这就是 strCommand
在调试中的样子:
您确定您的 C# 应用程序没有使用其他引擎,例如 SQL 服务器吗?在SQL服务器中,TO是保留字,所以应该用[To]代替。
TO也是Access中的保留字。尝试将其替换为 tbl_Org
.