ACCESS数据库,日期格式是2004-07-09 08:08:08,是YYYY-MM-DD HH:MM:SS的格式,现在我用二个datetimepicker控件选择日期,然后搜索数据库里面在这二个日期之间的数据,程序如下:
dataform.Qin.Close;//Qin是SQL查询的控件
dataform.Qin.SQL.Clear;
//in_thing 是要查找的数据库 in_time是日期,格式上面所说
dataform.Qin.SQL.Add('select * from in_thing where in_time between '+quotedstr('#'+datetimepicker1.Date+'#')+' and '+quotedstr('#'+datetimepicker2.Date+'#')+' order by id desc'); try
dataform.Qin.Open;
except
showmessage('查找失败!');
exit;
end;谢谢各位了,在线等候,OK马上给分。
dataform.Qin.Close;//Qin是SQL查询的控件
dataform.Qin.SQL.Clear;
//in_thing 是要查找的数据库 in_time是日期,格式上面所说
dataform.Qin.SQL.Add('select * from in_thing where in_time between '+quotedstr('#'+datetimepicker1.Date+'#')+' and '+quotedstr('#'+datetimepicker2.Date+'#')+' order by id desc'); try
dataform.Qin.Open;
except
showmessage('查找失败!');
exit;
end;谢谢各位了,在线等候,OK马上给分。
如果用参数写也行:
dataform.Qin.SQL.Add('select * from in_thing where in_time >= :sdate and in_time < =;edate order by id desc');
dataform.Qin.parambyname('sdate').asdatetime=strtodatetime(edit1.text);
dataform.Qin.parambyname('edate').asdatetime=strtodatetime(edit2.text);dataform.Qin.open;
TO: chwdong(chwdong)。开始我也想用between....and.但是老出错。
dataform.Qin.SQL.Clear;
//in_thing 是要查找的数据库 in_time是日期,格式上面所说
dataform.Qin.SQL.Add('select * from in_thing where in_time between '''+'''#'+datetimepicker1.Date+'#'''+' and '+'''#'+datetimepicker2.Date+'#'''+' order by id desc'); try
dataform.Qin.Open;
except
showmessage('查找失败!');
exit;
end;
去掉两个转换函数,还有在引号内,两个单引号才等于一个单引号。
dataform.Qin.SQL.Add('select * from in_thing where in_time between ''#'+datetostr(datetimepicker1.Date)+'#'' and ''#'+datetostr(datetimepicker2.Date)+'#'' order by id desc');
是这样的:
dataform.Qin.SQL.Add('select * from in_thing where in_time between '+'#'+datetostr(datetimepicker1.Date)+'#'+' and '+'#'+datetostr(datetimepicker2.Date)+'#'+' order by id desc');