delphi中如何用adotable进行条件查询

来源:百度知道 编辑:UC知道 时间:2024/09/28 06:13:39
在delphi中在DBGrid中按查询条件显示数据,如果用adoquery的话用select * from tablename where name='tom'这种形式就可以了,那用table的话怎样查询呢

无论你用什么控件连接数据库,查询语句都是用SQL,这个是不会错的,但是由于table没有SQL属性,所以还是需要用adoquery控件来输入查询语句了,在SQL属性里面,如果直接只用table连接数据库,那么只有在别的控件下,比如按钮的触发事件中来查询了。
也许直接用table也可以实现,但是我不会,请高手指正

adoconnection设置connectstring属性,连接数据库, loginprompt设为false, active设为true
adotable connection属性设为 adoconnection1, table name 设为你数据库中的表,active 设为true
datasource 的dataset 属性设为 adotable1
dbgrid 的datasource设为 datasource1
button click 事件写
procedure TForm1.Button1Click(Sender: TObject);
begin
adotable1.filtered := false;
adotable1.filter := '字段1 = ' + edit1.text;
if trim(edit1.text) <> '' then
adotable1.filtered := true;
end;

在ADOTable的Filter属性中输入查询条件:name='tom',再把Filtered属性值设为True就可以了。