delphi中查询问题

来源:百度知道 编辑:UC知道 时间:2024/05/12 05:22:41
我用的是adoquery来实现查询,在combobox中先择查询字段,在edit输入查询条件,查询结果显示在DBGrid中,请问一下如何实现,请高手指点迷津,谢谢!

添加一个DataSource1。DataSource1的Dateset设为adoquery1,把DBGrid的DataSource设为DataSource1,在button添加下面代码:
with adoquery1 do
begin
close;
sql.Clear;
sql.Text:='select * from 表名'+'where '+quotedstr(combobox1.text)+'='+quotedstr(edit1.text);
open;

select * from 表 where '+combobox1.text+'='+edit1.text+'(若字段为文本型则:'''+edit1.text+''')

if ADOQuery1.Active then
ADOQuery1.Active := False;
ADOQuery1.SQL.Add('select * from well_basic where '+ComboBox1.Text+'=:b');
ADOQuery1.Parameters.ParamValues['b'] := Edit1.Text;
//这两句可以可以防止出现查询类型错误。

ADOQuery1.Open;

with adoquery1 do
begin
close;
sql.Clear;
sql.Text:='select * from 表名'+
'where '+combobox1.text+'='''+edit1.text+'''';
open;
end;

dbgrid1.datasource=datasource1;
datasource1