Sql 为什么Access在这里要求参数arg?

Sql 为什么Access在这里要求参数arg?,sql,ms-access-2007,query-parameters,Sql,Ms Access 2007,Query Parameters,为了测试针对MS Access“数据库”的特定查询是否应返回任何记录(不是),我在Access中运行它,如下所示: SELECT TOP 5 t_accounts.account_no as AccountID, IIF(ISNULL(t_accounts.name),'[blank]',t_accounts.name) AS Name FROM t_accounts INNER JOIN td_department_accounts ON (t_accounts.account_no =

为了测试针对MS Access“数据库”的特定查询是否应返回任何记录(不是),我在Access中运行它,如下所示:

SELECT TOP 5 t_accounts.account_no as AccountID, IIF(ISNULL(t_accounts.name),'[blank]',t_accounts.name) AS Name 
FROM t_accounts 
INNER JOIN td_department_accounts ON (t_accounts.account_no = td_department_accounts.account_no) 
WHERE (AccountID >= 1) AND type = 'DE'
“Top 5”和“AccountID>=1”是我在代码中使用的硬编码版本:

cmd.CommandText = 
        string.Format(
        @"SELECT TOP {0} t_accounts.account_no as AccountID, IIF(ISNULL(t_accounts.name),'[blank]',t_accounts.name) AS Name 
        FROM t_accounts 
        INNER JOIN td_department_accounts ON (t_accounts.account_no = td_department_accounts.account_no) 
        WHERE (AccountID >= @firstId) AND type = 'DE'", CountToFetch);
    cmd.CommandType = CommandType.Text;
    cmd.Parameters.AddWithValue("@firstId", FirstId);
然而Access提示我:

(如果我输入“1”,它会返回一些记录)

我必须做些什么才能让Access的查询运行过程正常运行?还是我有眩晕

更新 也许有一个线索是,在上面显示的C代码中,我得到了“一个或多个必需的参数没有给出值。”为什么?唯一的参数是firstId,它的值确实是提供的

更新2 尽管它可以工作,但对其运行代码分析会导致该工具皱眉、皱眉和咆哮:

CA2100  Review SQL queries for security vulnerabilities The query
string passed to 'OleDbCommand.CommandText.set(string)' in
'NRBQSQLRepository.GetNDepartmentsFromID(string, int)' could contain
the following variables 'CountToFetch'. If any of these variables
could come from user input, consider using a stored procedure or a
parameterized SQL query instead of building the query with string
concatenations. 

当您打开参数时,Access将显示“输入参数值”对话框 对象,该对象包含无法访问的标识符或表达式 解释

在这种情况下,
AccountID
t\u accounts.account\u no
的字段别名。您试图引用Where子句中的字段别名。你不能那样做

改变

其中(AccountID>=1)和类型='DE'


其中(t\u accounts.account\u no>=1)和type='DE'

也许t\u accounts是一个需要参数的视图。我认为access支持这样的功能:不,t_accounts是一个表。我可以发誓,
是access中参数的占位符。@ConradFrix您可以在access查询中使用任何字符串作为参数。现在,如果您是通过ADO访问的,或者您是使用参数创建临时查询的,那就另当别论了。。我会确保
CountToFetch
是一个整数值,并且超过代码分析中的警告