看看这些代码:
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, DBTables, Grids, DBGrids, StdCtrls, QuickRpt, QRCtrls,
ExtCtrls, DBCtrls;type
TForm1 = class(TForm)
DataSource1: TDataSource;
Query1: TQuery;
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
DBGrid1: TDBGrid;
ComboBox1: TComboBox;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
s,s1:TdateTime;
implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);begin
if combobox1.text='' then
begin
showmessage('请选择数据表');
exit;
end
else
begin
s:=strtodatetime(edit1.Text);
s1:=strtodatetime(edit2.Text);
query1.SQL.Text:='select * from '''+combobox1.text+''' where csrq>='''+s+''' and csrq<='''+s1+'''';
query1.ExecSQL;
Query1.Close;end;
为什么老出现如此结果:
[Error] Unit1.pas(47): Incompatible types: 'String' and 'TDateTime'
[Error] Unit1.pas(47): Incompatible types我应该怎么解决!!!各位高手赐教!!!
其中 csrq为表中的字段:测试日期
end;end.
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, DBTables, Grids, DBGrids, StdCtrls, QuickRpt, QRCtrls,
ExtCtrls, DBCtrls;type
TForm1 = class(TForm)
DataSource1: TDataSource;
Query1: TQuery;
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
DBGrid1: TDBGrid;
ComboBox1: TComboBox;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
s,s1:TdateTime;
implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);begin
if combobox1.text='' then
begin
showmessage('请选择数据表');
exit;
end
else
begin
s:=strtodatetime(edit1.Text);
s1:=strtodatetime(edit2.Text);
query1.SQL.Text:='select * from '''+combobox1.text+''' where csrq>='''+s+''' and csrq<='''+s1+'''';
query1.ExecSQL;
Query1.Close;end;
为什么老出现如此结果:
[Error] Unit1.pas(47): Incompatible types: 'String' and 'TDateTime'
[Error] Unit1.pas(47): Incompatible types我应该怎么解决!!!各位高手赐教!!!
其中 csrq为表中的字段:测试日期
end;end.
query1.SQL.Text:='select * from '''+combobox1.text+''' where csrq>='''+s+''' and csrq<='''+s1+'''';
改为:
query1.SQL.Text:='select * from '+Trim(combobox1.text)+' where csrq>='''+DateTimeToStr(s)+''' and csrq<='''+DateTimeToStr(s1)+'''';
因为在select语句中表的两边不用''括起来,日期型变量不能跟字符串进行相加
//s:=strtodatetime(edit1.Text);
//s1:=strtodatetime(edit2.Text);
query1.SQL.Text:='select * from '''+combobox1.text+''' where csrq>='''+edit1.Text+''' and csrq<='''+edit2.Text+'''';
query1.ExecSQL;
//s:=strtodatetime(edit1.Text);
//s1:=strtodatetime(edit2.Text);
query1.SQL.Text:='select * from '''+combobox1.text+''' where csrq>='''+edit1.Text+''' and csrq<='''+edit2.Text+'''';
query1.ExecSQL;这样子我试过了!csrq与edit1.Text类型不匹配!
query1.SQL.Text:='select * from '+combobox1.text+' where csrq >= :date1 and csrq <= :date2';
query1.parambyname('date1').asdatetime := strtodatetime(edit1.Text);
query1.parambyname('date2').asdatetime := strtodatetime(edit2.Text);
query1.Open;
end;
目标:保证日期的格式正确!!!!
程序已经通了,不过,运行时出现这样的错误:
project project1.exe raised exception class EDBEngineError weth message'General SQL error[microfot][ODBC Visual FoxPro Driver]Operator/operand type mismatch!
s:=strtodatetime(edit1.Text);
s1:=strtodatetime(edit2.Text);
注释掉
因为‘+’符号必须是字符型才能操作
你的‘csrq’字段虽然是日期型的,也没有必要转换,服务器会自动识别的
s1:=strtodatetime(edit2.Text);
改为:
s:=edit1.text;
s1:=edit2.text;edit1和edit2输入时要按数据库中的时间格式!!!