Delphi,MS Access,ADO:“对带参数的查询的查询”没有给出正确的结果

时间:2022-10-29 11:10:35

Please help me to understand what is happening. Suppose there are following items in an Access database:

请帮我理解发生了什么。假设Access数据库中有以下项目:

table MyTable: IDLine (auto-increment field), a, b (integers), and there is a line with IDLine = 4 in this table.

表MyTable:IDLine(自动增量字段),a,b(整数),此表中有一行IDLine = 4。

query MyQuery:

查询MyQuery:

 SELECT IDLine, a+b AS Sum
 FROM MyTable
 WHERE IDLine >= :MinLine;

- i.e. a query that counts some statistics for MyTable and also selects some lines using a parameter.

- 即一个查询MyTable的某些统计信息的查询,并使用参数选择一些行。

Then in Delphi a TADOQuery is created:

然后在Delphi中创建一个TADOQuery:

SELECT *
FROM MyQuery
WHERE IDLine = :IDLine;

and the program is as follows:

程序如下:

ADOQuery1.Close;
ADOQuery1.Parameters.ParamByName('MinLine').Value := 2;
ADOQuery1.Parameters.ParamByName('IDLine').Value := 4;
ADOQuery1.Open;
ShowMessage(IntToStr(ADOQuery1.RecordCount));

For some reason the message gives 0, but there should be 1 line. Why is this happening?

由于某种原因,消息给出0,但应该有1行。为什么会这样?

If I change the text of ADOQuery1 to

如果我将ADOQuery1的文本更改为

SELECT *
FROM MyQuery
WHERE IDLine = 4;

the message gives 1. Also, if I delete the last line (with MinLine parameter) from MyQuery in Access, the message gives the proper result as well - does this mean that the problem is somehow connected with the parameter? But I can't get rid of the MinLine parameter in MyQuery, in fact this is just a subquery of an INSERT-INTO construction. What am I doing wrong and how can I solve this problem?

消息给出1.此外,如果我从Access中的MyQuery删除最后一行(使用MinLine参数),该消息也会给出正确的结果 - 这是否意味着问题以某种方式与参数相关联?但我无法摆脱MyQuery中的MinLine参数,实际上这只是INSERT-INTO结构的子查询。我做错了什么,如何解决这个问题?

1 个解决方案

#1


0  

The problem is solved by means of changing the order of the parameters to MinLine, then IDLine. Thank you for the answers.

通过将参数的顺序更改为MinLine,然后更改IDLine来解决该问题。谢谢你的回答。

#1


0  

The problem is solved by means of changing the order of the parameters to MinLine, then IDLine. Thank you for the answers.

通过将参数的顺序更改为MinLine,然后更改IDLine来解决该问题。谢谢你的回答。