我想做一个数据库的动态查询---可以选择表中的一个或者几个字段,需要用
adoconenction,aodquery来实现。
 例如:假设都是字符型, 
   如果checkbox1被选中则字段kind='''+Edit1.text+''' 
       checkbox2被选中则字段material='''+Edit2.text+''' 
       checkbox3被选中则字段type='''+Edit3.text+''' 
sqltext:=kind='''+Edit1.text+''' and  material='''+Edit2.text+'''   and type='''+Edit3.text+'''  几个条件必须连接起来,组合查询。
   我只试了一个条件的查询,sql语句没有错,但是查询不出结果,一直找不出问题所在!
   请各位大虾赐教!解决立刻结贴。

解决方案 »

  1.   


       adoquery1.close;
       adoquery1.sql.clear;
       if checkbox1.checked then
           adoquery1.sql.add('select * from table where kind='''+Edit1.text+'''');
       if checkbox2.checked then
           adoquery1.sql.add('select * from table where material='''+Edit2.text+'''');
       if checkbox3.checked then
           adoquery1.sql.add('select * from table where type='''+Edit3.text+''' ');
       adoquery.open ;
       adoquery.close;
      

  2.   

    我不是这个意思,我是想生成一条sqltext语句,然后用这一条语句可以做多次查询。
     既只要选择某个或者是某几个字段,输入值以后就可以查询
      例如:如果我只要用一个字段查询checkbox1.checked
       sql.text:='select * from table where type='''+Edit1.text+'''' 
      假如使用两个字段查询(checkbox1.checked and checkbox2.checked):
       sql.text:='select * from table where type='''+Edit1.text+''' and kind='''+edit2.text+''' '
      多条件依次类推,我现在想要的就是这条语句,既可以通过checkbox选择需要的字段(通过编辑框对子段赋值),动态生成一条sql语句,用这一条语句去查询。   close;
       adoquery1.sql.clear;
       adoquery1.sql.text:=sqlstr;
       ///需要的就是怎样生成这一条语句(sqlstr),而且能查询出结果(我做的语句已经生成正确了,但是查询不出结果,实际上记录存在。)
       adoquery1.prepared;
       adoquery1.open;
      

  3.   

    with adoquery1.close do
         close;
      if checkbo1.checked then  sql.add('select * from tablename where kind='''''edit1.text+'''');
       if checkbox2.checked then  sql.add('select * from tablename where material='''''edit2.text+''''); 
       if checkbo3.checked then  sql.add('select * from tablename where type='''''edit3.text+''''); 
       open;
        
      

  4.   

    sql.text:='select * from table where 1=1 '+'and yourfield=???'关键是要有1=1;这样条件就可以定义了;
      

  5.   

    1.假设你所有的查询条件EDIT组件在面板Panel1上.procedure Query;
    var
      aSQL: string
      i: integer;
      hasCondition: boolean;
    begin
      hasCondition := false;
      aSQL := 'SELECT * FROM TABLENAME';
      for i := 0 to CheckBox.Items.Count - 1 do
      begin
        if CheckBox.Checked[i] then
        begin  
          if not hasCondition then
          begin
            aSQL := aSQL + ' WHERE ';
            hasCondition := true;
          end;
        
          aSQL := aSQL + CheckBox.Items[i] + ' = ' + TEdit(Panel1.Control[i]).Text
          
          if i <> CheckBox.Items.Count - 1 then
            aSQL := aSQL + ' AND ';     
        end;
      end;  with AdoQuery1 do
      begin
        Sql.clear;
        Sql.Add(aSQL);
        try
          Open;
        except
          //handle your exception 
        end;
      end;
    end;
      

  6.   

    adoquery1.close;
       adoquery1.sql.clear;
    adoquery1.sql.add('select * from table where 1=1')   
    if checkbox1.checked then
           adoquery1.sql.add('and  kind='''+Edit1.text+'''');
       if checkbox2.checked then
           adoquery1.sql.add('and material='''+Edit2.text+'''');
       if checkbox3.checked then
           adoquery1.sql.add('and type='''+Edit3.text+''' ');
       adoquery.open ;
       adoquery.close;
      

  7.   

    把上面的最后的adoquery.close;去掉 不好意思的
      

  8.   

    严重同意 chuchenggang() 的
      

  9.   

    很简单
    var
      Str:string;
      
      Str := '';
      if 条件1 then
      begin
        if Str<>'' then
          Str := Str+' and ';
        Str := 条件1语句;
      end;
      if 条件2 then
      begin
        if Str<>'' then
          Str := Str+' and ';
        Str := 条件2语句;
      end;
      if 条件3 then
      begin
        if Str<>'' then
          Str := Str+' and ';
        Str := 条件3语句;
      end;
      ...........  SQL := ....+Str+....;
      

  10.   

    可以加一些标志,来辅助进行判断。
    比如如果checkbox1的onclick事件里,如果checked:=true,设一标志Ifcheck1:=true;
    判断 checkbox2,checkbox3...的标志IfCheck2,IfCheck3....是否为true
    如果是的话sql.text:=sql.text+' and kind='''+Edit1.text+'''';
      如果为否的话:sql.text:=sql.text+' where kind='''+Edit1.text+'''';  
    当查询出结果后置所有标志为false;
    同理checkbox2.........标志位要为全局变量
      

  11.   

    问题解决了,用的angle097113(深思不解) 的,看了各位提供的方法,大致意思都差不多。呵呵,谢谢各位了。立刻结贴。