我有一个combobox,一个edit,一个button,一个dbgrid,连接数据库的adocon和adoquery!
我想在combobox中选择一项作为列名,然后在edit中输入查询!!在dbgrid中显示结果;
我用以下sql语句可实现:
select * from 表 where ‘+combobox1.text+‘='''+edit1.text'''
但是我想在combobox中显示的是中文列名,但是我的数据库列名是英文!!这么解决!!
谢谢高手指教!
另外:我要模糊查询是不是要在edit1.text两边加%??
我想在combobox中选择一项作为列名,然后在edit中输入查询!!在dbgrid中显示结果;
我用以下sql语句可实现:
select * from 表 where ‘+combobox1.text+‘='''+edit1.text'''
但是我想在combobox中显示的是中文列名,但是我的数据库列名是英文!!这么解决!!
谢谢高手指教!
另外:我要模糊查询是不是要在edit1.text两边加%??
这句SQL语句很强大!这样行吗?如果行,那么你写个函数,一个string参数,根据参数不同输出相应的英文
函数返回该英文,然后where后面跟该函数的返回值
用以下代码可实现!
procedure Thzmsgfrm.Button1Click(Sender: TObject);
var
s:string;
begin
if combobox1.ItemIndex=0 then s:='select * from hz_msg where hz_id like ''%'+edit1.Text+'%''';
if combobox1.ItemIndex=1 then s:='select * from hz_msg where hz_name like ''%'+edit1.Text+'%''';
if combobox1.ItemIndex=2 then s:='select * from hz_msg where hz_sex like ''%'+edit1.Text+'%''';
if combobox1.ItemIndex=3 then s:='select * from hz_msg where hz_age like ''%'+edit1.Text+'%''';
if combobox1.ItemIndex=4 then s:='select * from hz_msg where hz_class like ''%'+edit1.Text+'%''';
if combobox1.ItemIndex=5 then s:='select * from hz_msg where jb_class like ''%'+edit1.Text+'%''';
if combobox1.ItemIndex=6 then s:='select * from hz_msg where ry_date like ''%'+edit1.Text+'%''';
if combobox1.ItemIndex=7 then s:='select * from hz_msg where cy_date like ''%'+edit1.Text+'%''';
if combobox1.ItemIndex=8 then s:='select * from hz_msg where fz_doc like ''%'+edit1.Text+'%''';
if combobox1.ItemIndex=9 then s:='select * from hz_msg where lx_name like ''%'+edit1.Text+'%''';
if combobox1.ItemIndex=10 then s:='select * from hz_msg where lx_phone like ''%'+edit1.Text+'%''';
if combobox1.ItemIndex=11 then s:='select * from hz_msg where lx_address like ''%'+edit1.Text+'%''';
if combobox1.ItemIndex=12 then s:='select * from hz_msg where re like ''%'+edit1.Text+'%''';
begin
hzaddfrm.adoq1.Close;
hzaddfrm.adoq1.SQL.Clear;
hzaddfrm.adoq1.SQL.Add(s);
hzaddfrm.adoq1.Open;
end;
end;不知道是否还有更好的方法!UP
第二个关于引号的问题可以利用 QuotedStr function来解决引号容易出错的问题
var
s:string;
begin
s := s + 'select * from hz_msg where ';
case combobox1.ItemIndex of
begin
0: s:= s + 'hz_id ';
1: s:= s + 'hz_name ';
2: s:= s + 'hz_sex ';
3: s:= s + 'hz_age ';
4: s:= s + 'hz_class ';
...
end;
s := s + 'like ''%'+edit1.Text+'%''';
with hzaddfrm.adoq1 do
begin
Close;
SQL.Clear;
SQL.Add(s);
Open;
end;
end;