大家帮忙看看这段代码,是日期这里的参数错了,但不知怎样改才对。
procedure TForm1.Button1Click(Sender: TObject);
var
  querystr:string;
begin
  querystr:='select * from patient where 1>0';
  if checklistbox1.Checked[0] then
  querystr:=querystr+'and 检查号 like '+quotedstr('%'+edit1.Text+'%')+'';
  if checklistbox1.Checked[1] then
  querystr:=querystr+'and 姓名 like '+quotedstr('%'+edit2.Text+'%')+'';
  if checklistbox1.Checked[2] then
  querystr:=querystr+'and 科别 like '+quotedstr('%'+combobox1.Text+'%')+'';
  if checklistbox1.Checked[3] then
  querystr:=querystr+'and 检查部位 like '+quotedstr('%'+combobox2.Text+'%')+'';
  if checklistbox1.Checked[4] then
  querystr:=querystr+'and 报告医师 like '+quotedstr('%'+combobox3.Text+'%')+'';
  if checklistbox1.Checked[5] then
  begin
    querystr:=querystr+'and 报告时间 between :qm and :hm'';
    parameters.ParamByName('qm').Value :=formatdatetime('yyyy-mm-dd',datetimepicker1.Date );
    parameters.ParamByName('hm').Value :=formatdatetime('yyyy-mm-dd',datetimepicker2.Date );
  end
  with adoquery1 do
  begin
    close;
    sql.clear;
    sql.add(querystr);
    open;
  end;
end;

解决方案 »

  1.   

    querystr:=querystr+'and 报告时间 between ?qm and ?hm'';改成?号试试,或者再试试@号querystr:=querystr+'and 报告时间 between @qm and @hm'';
      

  2.   

    querystr:=querystr+'and 报告时间 between '+chr(39)+formatdatetime('yyyy-mm-dd',datetimepicker1.Date )+chr(39)+' and '+chr(39)+formatdatetime('yyyy-mm-dd',datetimepicker2.Date )+chr(39); 
      

  3.   

    示例:
    var
        r1:string;
      begin
        with   adoquery1   do
        begin
            close;
            sql.clear;
            sql.add('select *  from aa where pid=:r1');
            parameters.ParamByname('r1').value:='1';
            showmessage(sql.Text);
            open;
        end;
      

  4.   

    querystr:=querystr+'and 报告时间 between :qm and :hm'';为什么多了一个单引号?
     
    如果不考虑SQL注入,倒是建议你直接把 formatdatetime('yyyy-mm-dd',datetimepicker1.Date ) 直接 代入。