急。急。急。有关ACESS数据库,如何将日期+时间做为WHERE的查询条件!格式应该怎么写。(delphi7.0)(30分)

  • 主题发起人 主题发起人 rpg-net
  • 开始时间 开始时间
R

rpg-net

Unregistered / Unconfirmed
GUEST, unregistred user!
procedure TYG_JB_Form.SAVE_BitBtnClick(Sender: TObject);
var
sqlstr: string;
date1: TDateTime;
date2: TDateTime;
date3: TDateTime;
date4: TDateTime;
begdate: TDateTime;
enddate: TDateTime;
//s :string;
begin
//s :=DateToStr(Start_DateTimePicker.DateTime)+' '+TimeToStr(DateTimePicker1.DateTime);
date1 := Start_DateTimePicker.DateTime;
date2 := StrToTime(FormatDateTime('hh:mm:ss', DateTimePicker1.DateTime));
begdate := date1 + date2;
date3 := Stop_DateTimePicker.DateTime;
date4 := StrToTime(FormatDateTime('hh:mm:ss', DateTimePicker2.DateTime));
enddate := date3 + date4;
try
with ADOQuery1 do
begin
Close;
SQL.Clear;
sqlstr := '';
//sqlstr :=' select sum(costmoney)from blacklog where beginTM=#2005-9-4#';
//sqlstr :=' select sum(costmoney)from blacklog where beginTM=#'+s+'#';
sqlstr := ' select sum(costmoney)from blacklog where beginTM=#' + DateToStr(begdate) + '#and endTM=#' + DateToStr(enddate) + '#group by costmoney';{问题就出现在着,如果我把datetostr换成datetimetostr就报错了}
//sqlstr :=' select sum(costmoney)from blacklog where beginTM=#'+DatetimeToStr(begdate)+'#';
SQL.Add(sqlstr);
open;(跟踪到这里报错)
end;
except
end;
end;
{我解析出来的SQL语句是:' select sum(costmoney)from blacklog where beginTM=#2005-9-24#and endTM=#2006-4-2#group by costmoney'}这种能过去,可不是我想要的格式,我想要的格式是日期+时间也就是(2005-9-24 11:10:23)格式的作为WHERE的查询条件。我用的控件全是DateTimePicker。时间显示是把DateTimePicker控件的KIND属性改成dtkTime。。。请各位高手指点一下。最好在我的代码上演示一下。看到我的帖子,有知道的加我QQ:543892448
邮箱:xulei_730@163.com
 
说说我的做法
var
tmpSQL: string;
DTBegin, DTEnd: TDateTime;
begin
DTBegin := Int(DateTimePicker1.Date) + Frac(DateTimePicker2.Time);
DTEnd := Int(DateTimePicker3.Date) + Frac(DateTimePicker4.Time);
tmpSQL := 'SELECT * FROM TABLE1 WHERE SJ BETWEEN :DTBEGIN AND :DTEND';
with ADOQuery1 do
begin
close;
sql.text := tmpSQL;
Parameters.ParamByName('DTBegin').Value := DTBegin;
Parameters.ParamByName('DTEnd').Value := DTEnd;
try
Open;
except
end;
end;
 
sqlstr :=' select sum(costmoney)from blacklog where beginTM= ''#2005-9-4#''';
 
sqlstr :=' select sum(costmoney) from blacklog where begin TM= #'2005-9-4'#';
 
//////japhe////////////////////////
var
tmpSQL: string;
DTBegin, DTEnd: TDateTime;
begin
DTBegin := Int(DateTimePicker1.Date) + Frac(DateTimePicker2.Time);
DTEnd := Int(DateTimePicker3.Date) + Frac(DateTimePicker4.Time);
tmpSQL := 'SELECT * FROM TABLE1 WHERE SJ BETWEEN :DTBEGIN AND :DTEND';
with ADOQuery1 do
begin
close;
sql.text := tmpSQL;
Parameters.ParamByName('DTBegin').Value := DTBegin;//这里就过不去报错误。。
Parameters.ParamByName('DTEnd').Value := DTEnd;
try
Open;
except
end;
end;
 
duhai_lee,LFCZ
你俩说的只是日期做为条件了,我要的是日期+时间SQL语句怎么写。。
 
我知道,不过分要300,这是一条难题
 
你也太狠了,行我忍栽了,你给我解决了我就给分。不过得我能调得过去的。。。我把我那个号的分也都给你了。。真黑。。。。。[:(!]
 
刚试验出来:
select QueCaption, EditDate from queall where editdate>=datevalue('2005-05-26')+timevalue('12:09:38') and editdate<datevalue('2005-05-26')+timevalue('12:09:39')

可以检索出来日期为:2005-05-26 12:09:38 的记录

给分吧!
 
///////yzxcsw/////////我是没有调试出来啊。你在我的代码上改一下。。
procedure TYG_JB_Form.SAVE_BitBtnClick(Sender: TObject);
var
sqlstr: string;
date1: TDateTime;
date2: TDateTime;
date3: TDateTime;
date4: TDateTime;
begdate: TDateTime;
enddate: TDateTime;
//s :string;
begin
//s :=DateToStr(Start_DateTimePicker.DateTime)+' '+TimeToStr(DateTimePicker1.DateTime);
date1 := Start_DateTimePicker.DateTime;
date2 := StrToDateTime(FormatDateTime('hh:mm:ss', DateTimePicker1.DateTime));
begdate := date1 + date2;
ShowMessage(datetimetostr(begdate));
date3 := Stop_DateTimePicker.DateTime;
date4 := StrToDateTime(FormatDateTime('hh:mm:ss', DateTimePicker2.DateTime));
enddate := date3 + date4;
ShowMessage(datetimetostr(enddate));
try
with ADOQuery1 do
begin
Close;
SQL.Clear;
sqlstr := '';
//sqlstr :=' select sum(costmoney)from blacklog where beginTM=#2005-9-4#';
//sqlstr :=' select sum(costmoney)from blacklog where beginTM=#'+s+'#';
sqlstr := ' select sum(costmoney)from blacklog where beginTM=#' + DatetimeToStr(begdate) + '#and endTM=#' + DatetimeToStr(enddate) + '#group by costmoney';
//sqlstr :=' select sum(costmoney)from blacklog where beginTM=#'+DatetimeToStr(begdate)+'#';
SQL.Add(sqlstr);
open;

end;

except

end;


end;
 
to rpg-net:我猜想你只是简单的我给出的代码拿过去测试而已。

你说的
Parameters.ParamByName('DTBegin').Value := DTBegin;//这里就过不去报错误。。
是因为必须有
'SELECT * FROM TABLE1 WHERE SJ BETWEEN :DTBEGIN AND :DTEND'
中的 table1 表 和 SJ 字段
还有ADOQUERY1必须连接好ACCESS数据库。

我用的ACCESS数据库中对时间查询测试 成功。
 
to:rpg-net
这是什么话,'SELECT * FROM TABLE1 WHERE SJ BETWEEN :DTBEGIN AND :DTEND'
我还不知道用。这个我要不知道,我就不在这问你们了。。你给的方法我真的测试了,本来就是调不过去吗。。不信明天我上班就把我的程序给你传过去,你看看。。开玩笑,你这有点骂人了呀。。郁闷。。。。。。[:(!]
 
xulei_730 is rpg-net就是我你要是帮我解决了,分数都给你,绝对不抵赖。。现在分数可以提高了。。帮我解决的给300。。。。
 
SORRY,或许我的措辞有点过激.如果可以的话,你可以把代码发到我邮箱
japhe@126.com,自当尽力而为.
 
你把Datetostr(begdate)和Datetostr(enddate)改成trunc(begdate)和trunc(enddate)
因为在delphi中Tdatetime本质上是实型。
 
to japhe;
我已经把程序发给你了,你帮我改一下吧。。注意我的数据库是ACESS。需要的话数据库给你发过去也可以。。打开程序可能打不开,我里边用了皮肤控件了你删除了就可以了。。
 
to京毛毛
你的方法我也试过了过不去。。
 
哎,难道就没有一个高手,能解决这个问题的吗???急死我了。。能解决给多少分我都愿意了。。真没人会啊。。郁闷。。。有需要的我把代码发给你们都可以。。。
 
邮件已收到,下午给答复。
 
我把你上述查询代码改了下
procedure TYG_JB_Form.SAVE_BitBtnClick(Sender: TObject);
var
sqlstr: string;
DTBegin, DTEnd: TDateTime;
begin
DTBegin := Int(Start_DateTimePicker.Date) + Frac(DateTimePicker1.Time);
DTEnd := Int(Stop_DateTimePicker.Date) + Frac(DateTimePicker2.Time);
sqlstr := ' select sum(costmoney) from blacklog where beginTM >= :DTBegin and endTM <= :DTEnd';
with ADOQuery1 do
try
Close;
SQL.Text := sqlstr;
Parameters.ParamByName('DTBegin').Value := DTBegin;
Parameters.ParamByName('DTEnd').Value := DTEnd;
open;
except
end;
end;

编译运行通过。
 
后退
顶部