我做多条件查询,在一个窗口上输入条件,在另外一个窗口上显示
下面是我的程序,各位看看有什么错误,有好的建议也不妨提出来,候教了!!procedure TForm5.Button1Click(Sender: TObject);begin
begin
with form6 do
begin
adoquery1.close;
adoquery1.sql.clear;
adoquery1.sql.add('select * from 教师表,课程表');
adoquery1.SQL.Add('where 教师表.课程序号=课程表.课程序号号'); if edit1.Text<>'' then
adoquery1.SQL.Add('and 教师表.教师编号:='''+edit1.Text+'''');
if edit2.Text<>'' then
adoquery1.SQL.Add('and 教师表.教师姓名:='''+edit2.Text+'''');
if combobox1.Text<>'' then
begin
adoquery1.SQL.Add('and 教师表.所属院系=:所属院系');
adoquery1.Parameters.ParamByName('所属院系').Value:=combobox1.Items.IndexOf(combobox1.Text);
end ;
if combobox2.Text<>'' then
adoquery1.SQL.Add(' and 课程表.课程名称:='''+combobox2.Text+'''');
adoquery1.ExecSQL; end;
form6.Show;
end;end;
下面是我的程序,各位看看有什么错误,有好的建议也不妨提出来,候教了!!procedure TForm5.Button1Click(Sender: TObject);begin
begin
with form6 do
begin
adoquery1.close;
adoquery1.sql.clear;
adoquery1.sql.add('select * from 教师表,课程表');
adoquery1.SQL.Add('where 教师表.课程序号=课程表.课程序号号'); if edit1.Text<>'' then
adoquery1.SQL.Add('and 教师表.教师编号:='''+edit1.Text+'''');
if edit2.Text<>'' then
adoquery1.SQL.Add('and 教师表.教师姓名:='''+edit2.Text+'''');
if combobox1.Text<>'' then
begin
adoquery1.SQL.Add('and 教师表.所属院系=:所属院系');
adoquery1.Parameters.ParamByName('所属院系').Value:=combobox1.Items.IndexOf(combobox1.Text);
end ;
if combobox2.Text<>'' then
adoquery1.SQL.Add(' and 课程表.课程名称:='''+combobox2.Text+'''');
adoquery1.ExecSQL; end;
form6.Show;
end;end;
最好调试一下
还要注意每个add语句里都第一个字符都要是空格
否则
比如说你的第一句“课程表”和where就连在一起了
会造成语法错误
select * from 教师表 a ,课程表 b
where a.课程序号=b.课程序号号
adoquery1.Parameters.ParamByName('所属院系').Value:=combobox1.Items.IndexOf(combobox1.Text);
combobox1.Items.IndexOf(combobox1.Text):这个得到的值是integer的!改成:adoquery1.sql.add('and 教师表.所属院系='''+combobox1.text);
2.在SQL语句中 判断条件用 "=",而不是":="
3.adoquery1.open返回查询结果,adoquery1.ExecSQL不返回结果
TForm5.Button1Click(Sender: TObject);
var
Sql:String;
begin
Sql:='select * from 教师表,课程表 where (教师表.课程序号=课程表.课程序号) '
if edit1.Text<>'' then
SQL:=Sql + 'and (教师表.教师编号='+ edit1.Text +') ';
if edit2.Text<>'' then
SQL:=Sql +'and (教师表.教师姓名='+edit2.Text+') ';
if combobox1.Text<>'' then
SQL:=Sql +'and (教师表.所属院系='+combobox1.Text+') ';
if combobox2.Text<>'' then
SQL:=Sql +'and (课程表.课程名称='+combobox2.Text+')';
if form6 = nil then
begin
Application.CreateForm(Tform6, form6);
with form6 do
begin
adoquery1.close;
adoquery1.sql.clear;
adoquery1.sql.add(SQL);
adoquery1.open;
end; form6.show;
end
else
begin
with form6 do
begin
adoquery1.close;
adoquery1.sql.clear;
adoquery1.sql.add(SQL);
adoquery1.open;
end;
form6.Show;
end;end;
说话的真多,不过我觉得NamasAmitabha(银雨辰)中的了!
应该可以结了
支持一下!
--------------------------------------
看见了么,
那支蛾子,
正飞向太阳,
那就是我!
--------------------------------------
只要求出在所选院系在下拉菜单中的位置即可,是不是使用items.indexof的属性啊,各位,能不能告诉我怎么做,直接在银雨辰上的程序改好了
SQL:=Sql +'and (教师表.所属院系='+inttostr(combobox1.Items.IndexOf(combobox1.Text))');
SQL:=Sql +'and (教师表.所属院系='+inttostr(ComboBox1.ItemIndex)+') ';