Delphi中的Ado查询过滤问题 [英] Ado query filtering problem in delphi

查看:150
本文介绍了Delphi中的Ado查询过滤问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码,当我搜索列中的数字时,它的工作正常

当我在列中搜索名称并且不同列中的日期时,我想要相同的代码认为

HI i have this code its work fine when i search a number inside column
I want the same code when i search a name in column and a date inside different column thinks

<pre lang="Delphi">
procedure TForm1.sEdit1Change(Sender: TObject);
 var a :integer;
begin
IF sEdit1.Text <> '' then
begin
a:=strtoint(sEdit1.Text);
adoquery1.active:=false;
adoquery1.sql.clear;
ADOQuery1.SQL.Add('select * from employé where num_emp='+inttostr(a));
adoquery1.open;
end
else
begin
adoquery1.active:=false;
adoquery1.sql.clear;
ADOQuery1.SQL.Add('select * from employé');
adoquery1.open;
end;
 end;   





我的尝试:



将代码更改为字符串和日期



What I have tried:

CHANGING the code to string and date

推荐答案

您只需要了解SQL语法。正如digimanus已经提到的,您可以在 WHERE 子句中使用 AND



但你应该使用参数化查询来避免 SQL注入 - 维基百科 [ ^ ]当用户输入数据时。这样也允许通过值而不是字符串传递数据,这对于日期(没有转换问题)和浮点值(没有舍入错误)特别有用。



未经测试的示例:

You just have to know about the SQL syntax. As already mentioned by digimanus you can use AND within WHERE clauses.

But you should use parametrised queries to avoid SQL injection - Wikipedia[^] when the data has been entered by users. Such allow also passing data by value instead of string which is especially useful with dates (no conversion problems) and floating point values (no rounding errors).

Untested example:
var
    Param : TParameter;
{ ... }

ADOQuery1.SQL.Add('SELECT * FROM employé WHERE name_column = :Name AND date_column = :Date');
Param := ADOQuery1.Parameters.ParamByName('Name');
Param.DataType := ftString;
Param.Value := varName;
Param := ADOQuery1.Parameters.ParamByName('Date');
Param.DataType := ftDate;
Param.Value := varDate;



相关链接:

ADOQuery(Delphi) - RAD Studio代码示例 [ ^ ]

TADOQuery.Parameters Property [ ^ ]



另请阅读使用过的数据库的SQL语法发动机。虽然基本语法是相同的,但是存在一些差异,例如引用标识符名称,当这些标识符是保留字时必须完成。如果数据库中有日期时间并且想要在没有时间的情况下匹配日期,则还必须这样做。每个数据库引擎都有不同的命令来获取日期时间的日期部分以进行比较。


Related links:
ADOQuery (Delphi) - RAD Studio Code Examples[^]
TADOQuery.Parameters Property[^]

Read also about the SQL syntax of the used database engine. While the basic syntax is the same there are some differences like for quoting identifier names which must be done when such identifiers are reserved words. You have to do that also if you have a datetime in the database and want to match a date without time. Each database engine has different commands to get the date portion of a datetime for comparison.


这篇关于Delphi中的Ado查询过滤问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆