比如我想在程序运行的时候运行这样一个SQL语句:select * from cust where name='张三' and sex='女'
在ADOQuery1.SQL.Add('select * from cust where ');中where后面如何表示'name' 'sex' '='等字段及符号
我试过以下语句行不通:begin
  var X:string;
  try
     with Form1 do
       begin
         ADOQuery1.Close;
         ADOQuery1.SQL.Clear;
         ADOQuery1.SQL.Add('select * from cust where'+':X');
         X:='FlatComboBox15.text+FlatComboBox16.text+FlatComboBox17.text+''+FlatComboBox18.text
            +''+FlatComboBox19.text+FlatComboBox20.text+FlatComboBox21.text;'
          //FlatComboBox为下拉框选择的字段,条件和符号.比如姓名、=、张三等等
         ADOQuery1.Parameters.ParamByName('X').value:=FlatComboBox16.text;
         ADOQuery1.Prepare; 
         ADOQuery1.OPEN;
       end;
   except
     showmessage('sorry!你的查询有误');
     exit;
  end;

解决方案 »

  1.   


    begin 
      var X:string; 
      try 
         with Form1 do 
           begin 
             ADOQuery1.Close; 
             ADOQuery1.SQL.Clear; 
             X := FlatComboBox15.text+FlatComboBox16.text+FlatComboBox17.text+' '+
               FlatComboBox18.text+' '+FlatComboBox19.text+FlatComboBox20.text+FlatComboBox21.text;
             ADOQuery1.SQL.Add('select * from cust where ' + X); 
             ADOQuery1.OPEN; 
           end; 
       except 
         showmessage('sorry!你的查询有误'); 
         exit; 
      end;
      

  2.   

    SQL.add('select * from cust where name=:name and sex=:sex');
    Parameters.ParamByName('name').value:=FlatComboBoxname.text; 
    Parameters.ParamByName('sex').value:=FlatComboBoxsex.text;
      

  3.   

    2楼的,我的SQL语句select * from cust where后面的字段是可选的,不是固定的,比如name和sex是根据FlatComboBox框里的选择内容而定的,象前面的语句可以是  select * from cust where name='张三' and sex='女' 
      select * from cust where sex='女' and  name='张三'
      select * from cust where name='张三' and sex='女' and age='19'
      select * from cust where name='张三'
     等等多种组合,
      
      

  4.   

    关键是表示问题,数据库里面的表字段是字母,而表单里的ComboBox选项是中文显示,
    select * from name='张三',如何把库里面的name字段跟ComboBox选出的‘姓名’对应起来
      

  5.   

    进行关联查询呀,你的字母与中文显示应该有一个表记录其对应关系吧。
    select * from a,b where a.id=b.id 类似这样的
      

  6.   

    同意4楼,可以采用拼SQL语句的方法
    var sqlstr:stringsqlstr:='select * from cust'
    if FlatComboBox1.text<>'' then
       sqlstr:=sqlstr+'where name='+'''+FlatComboBox1.text+'''
    if FlatComboBox2.text<>'' then
       sqlstr:=sqlstr+'and'+'''+FlatComboBox2.text+'''adoquery1.sql.add('sqlstr')这样做的话如果条件比较多就麻烦
      

  7.   

    楼上的,就是要多条件的啊!也就是在下拉框任意选字段和比较运算符号(< = > 包含等等)
    哎~~ 都没有一个满意的答案
      

  8.   

    就是拼SQL啊
    form开始的时候定义,或者是开始重新设定条件前:sqlstr='select * from cust where 1=1' (新增条件)BUTTON的单击事件里面加:
    //fieldComboBox.text  字段名
    //processComboBox.text 运算符
    //valueTextBox         值
    begin
    if (fieldComboBox.text <>'') and (processComboBox.text <>'') then 
       sqlstr:=sqlstr+'and '''+fieldComboBox.text+''' '+processComboBox.text +' '''+valueTextBox.text+''''; 
    adoquery1.sql.clear;
    adoquery1.sql.add(sqlstr) 
    end;可能单引号有不对,自己再调调!