procedure TFormPerson.btnSelectClick(Sender: TObject);
var
sSQL:string;
begin
sSQL := 'Select * From TB_PersonManage Where 1 > 0';
if Trim(edt1.Text) <> '' then
sSQL := sSQL + ' and HouseNum = ' + QuotedStr(edt1.Text) + '';
if Trim(edt2.Text) <> '' then
sSQL := sSQL + ' and RoomNum = ' + QuotedStr(edt2.Text) + '';
if Trim(edt3.Text) <> '' then
sSQL := sSQL + ' and WorkerNum = ' + QuotedStr(edt3.Text) + '';
if Trim(edt4.Text) <> '' then
sSQL := sSQL + ' and WorkerName = ' + QuotedStr(edt4.Text) + '';
if Trim(edt5.Text) <> '' then
sSQL := sSQL + ' and JoinDate = ' + QuotedStr(edt5.Text) + ''; //这条SQL报错
with Query do
begin
Connection := TDBConnector.Instance.Connection;
SQL.Text := sSQL;
Open;
ds1.DataSet := Query;
dbgrdh1.DataSource := ds1;
end;
end;
如果有日期条件的话就报错 参数JoinDate没有默认值,edt3.Text的值为2008-8-8
16 个解决方案
#1
access日期不是单引号 而是# 2008-9-26#
试下先.
试下先.
#2
是字符累加时有问题还是执行SQL语句时?
#3
sSQL := sSQL + ' and JoinDate = #' + (edt5.Text) + '#';
还是不行啊
还是不行啊
#4
?
#5
执行sql
#6
在DELPHI中,日期用单引号
sSQL := sSQL + ' and WorkerNum = ''' + QuotedStr(edt3.Text) + '''';
sSQL := sSQL + ' and WorkerNum = ''' + QuotedStr(edt3.Text) + '''';
#7
edt5.text是不是日期型,如果不是的话要转换下哦.
#8
QuotedStr就相当与加了引号啊
#9
执行SQL的时候好象都用字符类型吧
#10
no
类似于 '2008-07-01'
#11
相当与 Select ... And WorkerNum='2008-8-8'吧 应该是这样吧? 我照你说的加引号错了
#12
原来在DELPHI下测试过,日期型加单引号,自己试试吧,用一个日期字段来试
#13
Select * From TB_Person Where 1 > 0 And JoinDate=#2008-8-8#
Access这样写吗? 还是出错 参数JoinDate没有默认值
#14
按 12 楼提示,把 sSQL 变量 debug 出来看结果。
12楼说法正确的
12楼说法正确的
#15
记住,加的是两个单引号,不是1个双引号,别加错了
#16
你要是实在不行,直接用参数得了
procedure TFormPerson.btnSelectClick(Sender: TObject);
var
sSQL:string;
begin
sSQL := 'Select * From TB_PersonManage Where 1 > 0';
if Trim(edt1.Text) <> '' then
sSQL := sSQL + ' and HouseNum = ' + QuotedStr(edt1.Text) + '';
if Trim(edt2.Text) <> '' then
sSQL := sSQL + ' and RoomNum = ' + QuotedStr(edt2.Text) + '';
if Trim(edt3.Text) <> '' then
sSQL := sSQL + ' and WorkerNum = ' + QuotedStr(edt3.Text) + '';
if Trim(edt4.Text) <> '' then
sSQL := sSQL + ' and WorkerName = ' + QuotedStr(edt4.Text) + '';
if Trim(edt5.Text) <> '' then
sSQL := sSQL + ' and JoinDate = :myJoinDate';
with Query do
begin
Connection := TDBConnector.Instance.Connection;
SQL.Text := sSQL;
if Trim(edt5.Text) <> '' then
Parameters.ParamByName('myJoinDate').value:=QuotedStr(edt4.Text);
Open;
ds1.DataSet := Query;
dbgrdh1.DataSource := ds1;
end;
end;
#1
access日期不是单引号 而是# 2008-9-26#
试下先.
试下先.
#2
是字符累加时有问题还是执行SQL语句时?
#3
sSQL := sSQL + ' and JoinDate = #' + (edt5.Text) + '#';
还是不行啊
还是不行啊
#4
?
#5
执行sql
#6
在DELPHI中,日期用单引号
sSQL := sSQL + ' and WorkerNum = ''' + QuotedStr(edt3.Text) + '''';
sSQL := sSQL + ' and WorkerNum = ''' + QuotedStr(edt3.Text) + '''';
#7
edt5.text是不是日期型,如果不是的话要转换下哦.
#8
QuotedStr就相当与加了引号啊
#9
执行SQL的时候好象都用字符类型吧
#10
no
类似于 '2008-07-01'
#11
相当与 Select ... And WorkerNum='2008-8-8'吧 应该是这样吧? 我照你说的加引号错了
#12
原来在DELPHI下测试过,日期型加单引号,自己试试吧,用一个日期字段来试
#13
Select * From TB_Person Where 1 > 0 And JoinDate=#2008-8-8#
Access这样写吗? 还是出错 参数JoinDate没有默认值
#14
按 12 楼提示,把 sSQL 变量 debug 出来看结果。
12楼说法正确的
12楼说法正确的
#15
记住,加的是两个单引号,不是1个双引号,别加错了
#16
你要是实在不行,直接用参数得了
procedure TFormPerson.btnSelectClick(Sender: TObject);
var
sSQL:string;
begin
sSQL := 'Select * From TB_PersonManage Where 1 > 0';
if Trim(edt1.Text) <> '' then
sSQL := sSQL + ' and HouseNum = ' + QuotedStr(edt1.Text) + '';
if Trim(edt2.Text) <> '' then
sSQL := sSQL + ' and RoomNum = ' + QuotedStr(edt2.Text) + '';
if Trim(edt3.Text) <> '' then
sSQL := sSQL + ' and WorkerNum = ' + QuotedStr(edt3.Text) + '';
if Trim(edt4.Text) <> '' then
sSQL := sSQL + ' and WorkerName = ' + QuotedStr(edt4.Text) + '';
if Trim(edt5.Text) <> '' then
sSQL := sSQL + ' and JoinDate = :myJoinDate';
with Query do
begin
Connection := TDBConnector.Instance.Connection;
SQL.Text := sSQL;
if Trim(edt5.Text) <> '' then
Parameters.ParamByName('myJoinDate').value:=QuotedStr(edt4.Text);
Open;
ds1.DataSet := Query;
dbgrdh1.DataSource := ds1;
end;
end;