procedure TForm1.btn_countClick(Sender: TObject);
begin
    adoquery1.close;
    adoquery1.SQL.Clear;
    adoquery1.SQL.Add('select * from productcode where xdate between ''#'+datetostr(datetimepicker1.Datetime)+'#'' and ''#'+datetostr(datetimepicker2.Datetime)+'#'' ');
    adoquery1.ExecSQL;
    adoquery1.RecordCount;
end;报错内容是:
从字符串转换为datetime是发生语法错误请问怎么改正??
谢谢!!

解决方案 »

  1.   

    procedure TForm1.btn_countClick(Sender: TObject);
    begin
        adoquery1.close;
        adoquery1.SQL.Clear;
        adoquery1.SQL.Add('select * from productcode where xdate between ''#'+datetimetostr(datetimepicker1.Datetime)+'#'' and ''#'+datetimetostr(datetimepicker2.Datetime)+'#'' ');
        adoquery1.ExecSQL;
        adoquery1.RecordCount;
    end;
      

  2.   

    procedure TForm1.btn_countClick(Sender: TObject);
    begin
        adoquery1.close;
        adoquery1.SQL.Clear;
        adoquery1.SQL.Add('select * from productcode where xdate between '+chr(39)+datetimetostr(datetimepicker1.Datetime)+chr(39)+' and '+chr(39)+datetimetostr(datetimepicker2.Datetime)+chr(39)+');
        adoquery1.ExecSQL;
        adoquery1.RecordCount;
    end;
      

  3.   

    你把时间类型的转换成字符类型了当然会报错了
    这样写准没错
    with adoquery do begin
    Close;
    sql.clear;
    sql.text:='select * from productcode where xdate between :Q1 and Q2';
    Parameters.ParamByName('Q1').value:=datetimepicker1.Datetime;
    Parameters.ParamByName('Q2').value:=datetimepicker2.Datetime;
    open;
    end;
      

  4.   

    这样试试可不可以:
        adoquery1.SQL.Add('select * from productcode where xdate between #'+datetimetostr(datetimepicker1.Datetime)+'# and #'+datetimetostr(datetimepicker2.Datetime)+'#');
        adoquery1.Open;
      

  5.   


        adoquery1.close;
        adoquery1.SQL.Clear;
        adoquery1.SQL.Add('select * from productcode where xdate between '''+ FormatDateTime('yyyy-mm-dd',datetimepicker1.Datetime)+''' and '''+FormatDateTime(datetimetostr(datetimepicker2.Datetime+''');
        adoquery1.ExecSQL;
        adoquery1.RecordCount;