首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

变量查询字符串数据的有关问题

2012-03-05 
变量查询字符串数据的问题用ADOQuery和DataSource控件,Form上分别放置DBGrid、ComboBox、Edit、Button,DBGrid

变量查询字符串数据的问题
用ADOQuery和DataSource控件,Form上分别放置DBGrid、ComboBox、Edit、Button,
DBGrid里存放的是ID   Name   Sex   Age四个字段,现在需要进行条件查询,ComboBox的Items设置4个字段与ID   Name   Sex   Age分别对应:编号、姓名、性别、年龄。现在需要对ComboBox里的数据进行选择,然后在Edit里输入检索数据,DBGrid就会显示查询到的数据。
我的程序如下,但是提示“Form子句语法错误”   ,是不是SQL语句的错误,还是其他的代码我没有写,请各位指教,谢谢!
procedure   TForm1.Button1Click(Sender:   TObject);
var
    s1,s2:   String;
begin
    s1   :=   Trim(ComboBox1.Text);
    s2   :=   Trim(Edit1.Text);
    if   s1   =   ' '   then
    begin
        ShowMessage( '查询条件不能为空! ');
    end
    else   if   s2   =   ' '   then
    begin
        ShowMessage( '查询值不能为空! ');
        Edit1.SetFocus;
    end
    else
    begin
        with   ADOQuery1   do
        begin
            Close;
            SQL.Clear;
            SQL.Add( 'select   *   from   biao   where '   +s1+   '= '   + ' ' ' '   +Trim(s2)+ ' ' ' ');
            Open;
        end;
        if   ADOQuery1.RecordCount   >   0   then
            ShowMessage( '查询成功! ')
        else
            ShowMessage( '查询失败 ');
        end;
end;

[解决办法]
SQL.Add( 'select * from biao where ' +s1+ '= ' + ' ' ' ' +Trim(s2)+ ' ' ' ');
发生了错误
应该改为
SQL.Add( 'select * from biao where ' +s1+ '= ' + ' ' ' ' +Trim(s2)+ ' ' ' ');
也就是在 where 后面加一个空格,否则sql语句就错了

热点排行