SELECT语句中的OleDbCommand语法错误0x80040E14

时间:2017-02-03 05:01:11

标签: c# winforms oledbdataadapter

此查询在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在调试中的样子:

enter image description here

2 个答案:

答案 0 :(得分:2)

TO也是Access中的保留字。尝试将其替换为tbl_Org

答案 1 :(得分:-2)

你确定你的c#app没有使用其他引擎,比如SQL Server吗?在SQL Server中,TO是保留字,因此您应该使用[To]。