最近在做一个关于listbox的查询,遇到了一些问题。
就是把要查询listbox里面的所有值做查询条件,点查询按钮将值输出到TDBGRID里面?????请问如何做呢?

解决方案 »

  1.   

    for i:= 0 to ListBox1.Items.Count -1 do
      begin
        GetValue:= ListBox1.Items.Strings[i];
        {以GetValue为条件查询..执行插入到TDBGrid相关操作..}
      end
      

  2.   


    procedure TForm1.Button3Click(Sender: TObject);
    var
    str,s:string;
    i:integer;
    begin s :='select * from A where aname in';{查表中aname这个字段}
     for i :=0  to ListBox1.Items.Count-1 do
         if str='' then
            str:=str+''''+ListBox1.Items.Strings[i]+''''
         else
            str:=str+','''+ListBox1.Items.Strings[i]+'''';
     str:=s+'('+str+')'; with adoquery1 do begin
      close;
      sql.Text:=str;
      open;
     end;
    end;
      

  3.   

    for i:= 0 to ListBox1.Items.Count -1 do
      begin
        GetValue:= ListBox1.Items.Strings[i];
        {以GetValue为条件查询..执行插入到TDBGrid相关操作..}
      end
    这步会做,关键就这样循环如何执行SQL查询?请能否具体点好吗??谢谢!!
    with dataa.ADOQuery2 do
    begin
    close;
    sql.Clear;
    for i:=0 to listbox1.Count-1 do
    begin
    sql.Add('select * from 收入明细表 where shouruxiangmu=:a');
    Parameters.ParamByName('a').Value :=listbox1.Items[i];
    open;end;
    if dataa.ADOQuery2.RecordCount>0 then
    DataSource1.DataSet := Dataa.ADOQuery2;
    end;
      

  4.   


     for i :=0  to ListBox1.Items.Count-1 do
         if str='' then
            str:=str+''''+ListBox1.Items.Strings[i]+''''
         else
            str:=str+','''+ListBox1.Items.Strings[i]+'''';
     str:=s+'('+str+')';这不是拼查询语句吗?
      

  5.   

    SQL里新建临时表#TEMPshouruxiangmu
    for i:= 0 to ListBox1.Items.Count -1 do
      begin
        ADOQuery1.Sql.text:= 'insert into #TEMPshouruxiangmu values('''+
                             ListBox1.Items.Strings[i]+'''';
        ADOQuery1.excsql;
      end最后 select * from 收入明细表 where shouruxiangmu in (select * from #TEMPshouruxiangmu )
    一次返回所有的,这不就行了?或许会用重复,你就加上DISTINCT
      

  6.   


    var
    str,s:string;
    i:integer;
    begin s :='select * from 收入明细表 where shouruxiangmu in ';
     for i :=0  to ListBox1.Items.Count-1 do
         if str='' then
            str:=str+''''+ListBox1.Items.Strings[i]+''''
         else
            str:=str+','''+ListBox1.Items.Strings[i]+'''';
     str:=s+'('+str+')'; with adoquery1 do begin
      close;
      sql.Text:=str;
      open;
     end;
      

  7.   

    谢谢!搞定!!
    procedure TForm10.SpeedButton5Click(Sender: TObject);
    var
    str,s:string;
    i:integer;
    begin
    if radiogroup1.ItemIndex=0 then
    begin
     s :='select * from 收入明细表 where shouruxiangmu in';{查表中aname这个字段}
     for i :=0  to ListBox2.Items.Count-1 do
         if str='' then
            str:=str+''''+ListBox2.Items.Strings[i]+''''
         else
            str:=str+','''+ListBox2.Items.Strings[i]+'''';
     str:=s+'('+str+')'; with dataa.ADOQuery2 do begin
      close;
      sql.Text:=str;
      open;
     end;
     if dataa.ADOQuery2.RecordCount>0 then
     DataSource1.DataSet := Dataa.ADOQuery2;
     end
    else
    begin
     s :='select * from 支出明细表 where zhichuxiangmu in';
     for i :=0  to ListBox2.Items.Count-1 do
         if str='' then
            str:=str+''''+ListBox2.Items.Strings[i]+''''
         else
            str:=str+','''+ListBox2.Items.Strings[i]+'''';
     str:=s+'('+str+')'; with dataa.ADOQuery2 do begin
      close;
      sql.Text:=str;
      open;
     end;
     if dataa.ADOQuery2.RecordCount>0 then
     DataSource1.DataSet := Dataa.ADOQuery2;
    end;
    end;
      

  8.   

    GDTOPONE 
    IT民工   谢谢你!!