begin SQLTime:=True; s:='select * from jshjk.db where a<>nil'; for i := 0 to jshjcx.ComponentCount-1 do begin try str:=jshjcx.Components[i].Name; if ((jshjcx.Components[i] is Tcombobox) //判断是不是COMBOBOX且不能为时间相关的组合框 and((jshjcx.Components[i] as Tcombobox).text<>'))then if not((str=ComboboxYear.Name) or (str=ComboboxMonth.Name) or(str=ComboboxSecYear.Name)or (str=ComboboxSecMonth.Name) )then s:=s+' and '+copy(jshjcx.Components[i].Name,(Length(jshjcx.Components[i].Name)-3),4)+'=' //限制字段长度为了4 +(jshjcx.Components[i] as Tcombobox).text else //如果时间相关的年份有数据的话 if ((ComboboxYear.text<>') and SQLTime)then begin s:=s+' and hjsj between :date1 and :date2'; SQLTime:=false; end; if ((jshjcx.Components[i] is TEdit) //如果是文本框控件的话 and((jshjcx.Components[i] as TEdit).text<>'))then s:=s+' and '+copy(jshjcx.Components[i].Name,(Length(jshjcx.Components[i].Name)-3),4)+'=' //限制了字段长度必须为4 +(jshjcx.Components[i] as TEdit).text; except exit; end;
end; showmessage(s); //用于测试,到这一步没问题,应是S相加之后在执行SQL时出的问题 end; //设置完SQL语句之后开始查询 with query1 do begin Close; SQL.Clear; SQL.Add(s); ParamByName('date1').AsDate:= EncodeDate(StrToInt(ComboBoxYear.Text),StrToInt(ComboBoxMonth.Text),1); ParamByName('date2').AsDate:= EncodeDate(StrToInt(ComboBoxSecYear.Text),StrToInt(ComboBoxSecMonth.Text),30); prepare; open; end;
这两句话哪个对呢 select * from jshjk.db where a<>nil and jszb=语文组 select * from jshjk.db where a<>nil and jszb='语文组' 很可能是这个地方出的错知道的朋友请速回答
----------------------------------------------
无论你是狮子还是羚羊,为了生存,你别无选择,你必须跑!
procedure Tjshjcx.ButtonSearchClick(Sender: TObject); var i:integer; Str,s:String; SQLTime:boolean; begin //判断各选项是否为空 {if ((ComboBoxJszb.Text=')and(ComboBoxJsxm.Text=')and(EditJxmc.Text=') and(ComboBoxZxmc.Text=')and(ComboBoxjxlb.Text=')and(ComboBoxJxdj.Text=') and(ComboBoxYear.Text=')and(ComboBoxSecYear.Text='))then begin Application.MessageBox('当前查询输入为空,请输入查询项目!','提示',mb_ok); exit; end; } //判断起止年份是否填写 if (((comboboxYear.Text=')and(comboboxSecYear.Text<>')) or((comboboxYear.Text<>')and(comboboxSecYear.Text='))) then begin Application.MessageBox('年份设置不正确,请重新设置!','提示',mb_ok); exit; end; //判断起止年份是否正确2不能小于1 if ((comboboxYear.Text<>')and (comboboxSecYear.Text<>')) then if(StrToInt(comboboxYear.Text)>StrToInt(comboboxSecYear.Text)) then begin Application.MessageBox('起始年份数不能大于结束年份!','提示',mb_ok); exit; end; //判断各框是否为空,不为空的话作为查询参数输入到SQL语句中去 begin SQLTime:=True; s:='select * from jshjk.db where :a'; for i := 0 to jshjcx.ComponentCount-1 do begin try str:=jshjcx.Components[i].Name; if ((jshjcx.Components[i] is Tcombobox) //判断是不是COMBOBOX且不能为时间相关的组合框 and((jshjcx.Components[i] as Tcombobox).text<>'))then if not((str=ComboboxYear.Name) or (str=ComboboxMonth.Name) or(str=ComboboxSecYear.Name)or (str=ComboboxSecMonth.Name) )then s:=s+' and '+(copy(jshjcx.Components[i].Name,(Length(jshjcx.Components[i].Name)-3),4)+'='+''+(jshjcx.Components[i] as Tcombobox).text+'') else //如果时间相关的年份有数据的话 if ((ComboboxYear.text<>') and SQLTime)then begin s:=s+' and hjsj between :date1 and :date2'; SQLTime:=false; end; if ((jshjcx.Components[i] is TEdit) //如果是文本框控件的话 and((jshjcx.Components[i] as TEdit).text<>'))then s:=s+' and '+copy(jshjcx.Components[i].Name,(Length(jshjcx.Components[i].Name)-3),4)+'=' //限制了字段长度必须为4 +''+(jshjcx.Components[i] as TEdit).text+''; except exit; end;
end; showmessage(s); //用于测试,到这一步没问题,应是S相加之后在执行SQL时出的问题 end; //设置完SQL语句之后开始查询 with query1 do begin Close; SQL.Clear; SQL.Add(s); ParamByName('a').AsBoolean:=True; //ParamByName('date1').AsDate:= // EncodeDate(StrToInt(ComboBoxYear.Text),StrToInt(ComboBoxMonth.Text),1); // ParamByName('date2').AsDate:= // EncodeDate(StrToInt(ComboBoxSecYear.Text),StrToInt(ComboBoxSecMonth.Text),30); prepare; open; end;
{s:='select * from jshjk.db where '; begin for i:=0 to Self.ControlCount-1 do begin if ((controls[i] is TcomBobox) or (controls[i] is TEdit)) then begin str:=Controls[i].Name; if ((str<>ComboboxYear.Name) or (str<>ComboboxMonth.Name) or(str<>ComboboxSecYear.Name)or (str<>ComboboxSecMonth.Name) )then begin s:=s+controls[i] end; end; end; end;}
var OrAnd,S,SqlTemp:string; j:integer; begin //根据需要设置连接查询条件OrAnd为 or 或and 可用一个checkbox1 判断 if checkbox1.checked then OrAnd:='OR' else OrAnd:='OR';
SQLTime:=True; SqlTemp:='select * from jshjk.db where '; s:='; j:=0; for i := 0 to jshjcx.ComponentCount-1 do begin try str:=jshjcx.Components[i].Name; if ((jshjcx.Components[i] is Tcombobox) //判断是不是COMBOBOX且不能为时间相关的组合框 and((jshjcx.Components[i] as Tcombobox).text<>'))then if not((str=ComboboxYear.Name) or (str=ComboboxMonth.Name) or(str=ComboboxSecYear.Name)or (str=ComboboxSecMonth.Name) )then begin//注意这里这是我加的 if j>0 then s:=OrAnd+s ;//其它地方就不用再考虑查询条件了,你静态地加上了 and 如果需要加 or 你又怎么办了 //其它地方和这里差不多 s:=s+(copy(jshjcx.Components[i].Name,(Length(jshjcx.Components[i].Name)-3),4)+'='+''+(jshjcx.Components[i] as Tcombobox).text+''); end else //如果时间相关的年份有数据的话 if ((ComboboxYear.text<>') and SQLTime)then begin s:=s+' and hjsj between :date1 and :date2'; SQLTime:=false; end; if ((jshjcx.Components[i] is TEdit) //如果是文本框控件的话 and((jshjcx.Components[i] as TEdit).text<>'))then s:=s+' and '+copy(jshjcx.Components[i].Name,(Length(jshjcx.Components[i].Name)-3),4)+'=' //限制了字段长度必须为4 +''+(jshjcx.Components[i] as TEdit).text+''; except exit; end;
end; showmessage(s); //用于测试,到这一步没问题,应是S相加之后在执行SQL时出的问题 end; //设置完SQL语句之后开始查询 with query1 do begin Close; SQL.Clear; SQL.Add(sqlTemp+s);//这里做了修改 ParamByName('a').AsBoolean:=True; //ParamByName('date1').AsDate:= // EncodeDate(StrToInt(ComboBoxYear.Text),StrToInt(ComboBoxMonth.Text),1); // ParamByName('date2').AsDate:= // EncodeDate(StrToInt(ComboBoxSecYear.Text),StrToInt(ComboBoxSecMonth.Text),30); prepare; open; end;