关于ADO查询问题(100分)

  • 主题发起人 happyhandsome
  • 开始时间
H

happyhandsome

Unregistered / Unconfirmed
GUEST, unregistred user!
我用ADO连接了一个 ACESS数据库,然后放胃一个DATASOURCE 和DBGRIDE ,
和一个COMBOBOX1和EDIT1,现在我想在COMBOBOX1中输入字段,
在EDIT1中输入值,进行查询,然后符合要求在DBGRIDE中显示代码如何编写,请高手指点[:)]
 
听起来不难!
但帮不上你,我也刚开学!
 
将DATASOURCE和ADO关联 DATASOURCE和DBGRIDE 关联
用SQL语句
'select * from table1 where '+combobox1.text+'='+edit1.text
然后执行SQL语句就可以了!
 
你要注意字段类型,字符行的要加('')的
 
再加一个ComboBox2,items={等于 大于 小于 类似 不等于}
var
sign:string;
fstr:string;
begin
if Edit1.Text='' then
begin
Queryed:=False;
end;
case ComboBox2.ItemIndex of
0: sign:='=';
1: sign:='>';
2: sign:='<';
3: sign:=' like ';
4: sign:='<>';
end;
case ComboBox1.ItemIndex of
0,4:fstr:='select * from table1 where(%s%s%s) order by %s';
19: fstr:='select * from table1 where(%s%s#%s#) order by %s';
else fstr:='select * from table1 where(%s%s''%s'') order by %s';
end;
with ADOQuery1 do
begin
SQL.Clear;
SQL.Add(Format(fstr,[ComboBox1.Text,sign,Edit1.Text,ComboBox1.Text]));
Prepared;
Open;
end;
Queryed:=True;
end;
 
1.在SQL用变量
2.变量赋值 query.ParamByName().As....... 有利于类型的匹配

[:D]
 
应该是这样:
'select * from table1 where '+combobox1.text+'='+QuotedStr(edit1.text)
 
adoquery1.close;
adoquery1.sql.text:='select * from table1 where '+combobox1.text+'='''+edit1.text+'''';
adoquery1.open;
注意:單引號個數。
 
以上几位答案基本真确,应该给他们加分
 
顶部