This query runs perfectly in 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]
However when I try to execute it from my C#
winforms application, I get syntax error:
0x80040E14 Syntax error near keyword FROM
EDIT: My C# code:
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];
}
}
That's how strCommand
looks like in Debug:
TO is a reserved word in Access as well. Try to replace it to something like tbl_Org
.
Are you sure your c# app isn't using another engine, like SQL Server? In SQL Server, TO is a reserved word, so you should use [To] instead.
User contributions licensed under CC BY-SA 3.0