是否可以在Where子句以外的地方使用OleDBCommand参数?

时间:2014-07-15 23:32:40

标签: c# sql ms-access ms-access-2007 oledbcommand

我有这段代码:

using (OleDbCommand cmd = conn.CreateCommand())
{
    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);

...但是我想知道我是否也可以使用参数作为最高计数,例如:

using (OleDbCommand cmd = conn.CreateCommand())
{
    cmd.CommandText = 
        @"SELECT TOP @count 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'";
    cmd.CommandType = CommandType.Text;
    cmd.Parameters.AddWithValue("@firstId", FirstId);
    cmd.Parameters.AddWithValue("@count", CountToFetch);

...或者数据库参数仅限于WHERE子句吗?

更新

使用此代码:

  cmd.CommandText = 
        @"SELECT TOP @countToFetch 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'";
    . . .
  cmd.Parameters.AddWithValue("@firstId", FirstId);
  cmd.Parameters.AddWithValue("@countToFetch", CountToFetch);

...我得到了" SELECT语句包含拼写错误或缺失的保留字或参数名称,或者标点符号不正确。"

所以我又回到了:

   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.Parameters.AddWithValue("@firstId", FirstId);

更新2

此:

SELECT TOP (@countToFetch) 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'"
Access中的

...告诉我" SELECT语句包含拼写错误或缺失的保留字或参数名称,或者标点符号不正确。"

注意:邮差告诉我在进行REST调用时最终会产生该查询。

更新3

我也在Update 2中尝试了它,但是使用":"而不是" @"这样:

SELECT TOP (?) 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 >= ?) AND type = 'DE'"

1 个答案:

答案 0 :(得分:1)

我发现了this帖子似乎表明此 工作,至少使用T-SQL。考虑到这一点,我倾向于认为它适用于Access。就是说,请记住包括括号。

无论如何,参数只能在WHERE子句中,这绝对不是真的。您可以在SELECT,甚至是ORDER BY

中使用它们