数据库时间段查询

在数据库中定义日期类型字段,然后按此字段查询某时间段内的记录。在C++builder中用:select * from datatable where datefield between #2016-7-1 12:00:00# and #2016-7-20 12:00:00# 在Access2005中报错。因此,要用TADOquery的参数查询方法:
     TDateTime d1,d2,t1,t2,startTime,endTime;
     d1 = DateTimePicker1->Date;
     t1 = DateTimePicker2->Time;

     d2 = DateTimePicker3->Date;
     t2 = DateTimePicker4->Time;

     startTime = StrToDateTime(d1.FormatString("yyyy-MM-dd")+" "+t1.FormatString("hh:mm:ss")); //起始时间
     endTime   = StrToDateTime(d2.FormatString("yyyy-MM-dd")+" "+t2.FormatString("hh:mm:ss")); //结束时间
     //====================================================
     ADOQuery1->Active = false;
     ADOQuery1->SQL->Clear();
     ADOQuery1->SQL->Text = "select * from  data  where Format(SaveDatatime, 'yyyy-MM-dd HH:mm:ss') between :a and :b order by id";
     ADOQuery1->Parameters->ParamByName("a")->Value = startTime;
     ADOQuery1->Parameters->ParamByName("b")->Value = endTime;
     ADOQuery1->Active = true;

数据库时间段查询_第1张图片

你可能感兴趣的:(C++Builder6,Sqlserver数据库)