【问题标题】:Is it possible to use OleDBCommand parameters elsewhere than the Where clause?是否可以在 Where 子句之外的其他地方使用 OleDBCommand 参数?
【发布时间】:2014-07-15 23:32:39
【问题描述】:

我有这个代码:

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

我也像在更新 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'"

【问题讨论】:

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


【解决方案1】:

我看到this 的帖子似乎表明这个确实工作,至少在 T-SQL 中是这样。鉴于此,我倾向于假设它适用于 Access。就像上面所说的,记住要包含括号。

无论如何,参数只能在WHERE 子句中是绝对不正确的。您可以在SELECT 甚至ORDER BY 中使用它们。

【讨论】:

  • 我得到了,“SELECT 语句包含一个保留字或参数名称拼写错误或丢失,或者标点符号不正确。”
  • 该帖子说要在括号中包含参数:SELECT TOP (@countToFetch) t_accounts...。这样做效果更好吗?
  • 听起来不错。您可能还想在 Access 本身中尝试它。有时这可以毫无理由地提供有价值的见解。
  • 有趣。好吧,它当然有可能在 Access 中不可行。这有点令人惊讶,但我想它和 T-SQL 之间在某些地方必须存在差异。 Access 是否有像 T-SQL 那样的语法图?我怀疑这将是一个确定的来源。我只是看了一点,但我只能在 Office 上找到更多“用户友好”的页面,实际上并没有说任何有用的东西。
  • 我希望语法与其他参数的语法相同。我的意思是,这当然是一个有趣的问题。如果不查找它,我永远不会猜到或发现 SQL 需要在它周围的括号,所以它绝对有可能是它正在寻找的一些完全随机的指标。
猜你喜欢
  • 1970-01-01
  • 2012-04-24
  • 2014-05-19
  • 1970-01-01
  • 2017-01-04
  • 2018-09-19
  • 2015-06-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多