我正在开发一个用于记录用户活动的小子系统。系统采用作为数据库,Delphi7和ADO构建接口。
我遇到的问题是,我无法找到具有特定datetime值的记录。
以下是问题的样本再现:
1.数据库:MS 2005速成版。
-- Table creation
CREATE TABLE [tlog] (
[USERN] [numeric](10, 0) NULL,
[USERDATE] [datetime] NULL,
[LOGTEXT] [varchar](250) COLLATE Cyrillic_General_CS_AS NULL
);
-- Insert date/time value
INSERT INTO [tlog] (USERN, USERDATE, LOGTEXT)
VALUES (1, CURRENT_TIMESTAMP, 'Record current activity')
-- Insert date only value
INSERT INTO [tlog] (USERN, USERDATE, LOGTEXT)
VALUES (1, '20180202', 'Record current activity')
-- Table's content
-------------------------------------------------------------
| USERN | USERDATE | LOGTEXT |
-------------------------------------------------------------
| 1 | 26/10/2015 17:13:36.597 | Record current activity |
-------------------------------------------------------------
| 1 | 02/02/2018 00:00:00.000 | Record current activity |
-------------------------------------------------------------2.示例代码: Delphi 7和ADO
procedure TfrmMain.btnLocateClick(Sender: TObject);
var
d: TDateTime;
tblLog: TADOTable;
begin
//
ThousandSeparator := ' ';
DecimalSeparator := '.';
DateSeparator := '/';
ShortDateFormat := 'dd/mm/yyyy';
LongDateFormat := 'dd/mm/yyyy';
TimeSeparator := ':';
ShortTimeFormat := 'hh:mm';
LongTimeFormat := 'hh:mm';
TwoDigitYearCenturyWindow := 50;
ListSeparator := ';';
//
tblLog := TADOTable.Create(Application);
try
//
tblLog.ConnectionString :=
'Provider=SQLOLEDB.1;'+
'Password=xxxx;'+
'Persist Security Info=True;'+
'User ID=xxxxxxxx;'+
'Initial Catalog=xxxxxxxxx;'+
'Data Source=127.0.0.1\xxxxxxx,1066';
tblLog.TableName := '[tlog]';
tblLog.Open;
// First try - locate with exact value. NOT WORKING.
d := StrToDateTime('26/10/2015 17:13:36.597');
if tblLog.Locate('USERDATE', d, []) then
ShowMessage('Exact value, no Locate options: Located')
else
ShowMessage('Exact value, no Locate options: Not located');
if tblLog.Locate('USERDATE', d, [loPartialKey]) then
ShowMessage('Exact value, with Locate options: Located')
else
ShowMessage('Exact value, with Locate options: Not located');
// Second try - locate with value that matches format settings. NOT WORKING.
d := StrToDateTime('26/10/2015 17:13');
if tblLog.Locate('USERDATE', d, []) then
ShowMessage('Hours and minutes, no Locate options: Located')
else
ShowMessage('Hours and minutes, no Locate options: Not located');
if tblLog.Locate('USERDATE', d, [loPartialKey]) then
ShowMessage('Hours and minutes, with Locate options: Located')
else
ShowMessage('Hours and minutes, with Locate options: Not located');
// Locate with date only value. WORKING.
d := StrToDateTime('02/02/2018');
if tblLog.Locate('USERDATE', d, []) then
ShowMessage('Located')
else
ShowMessage('Not located');
finally
//
tblLog.Close;
tblLog.Free;
end;
end;3.预期结果:定位记录。
4.实际结果: TADOTable.Locate()返回false。
我做错了什么,如何将datetime值传递给TADOTable.Locate()方法?
提前感谢!
发布于 2018-02-02 13:46:20
您几乎正确地使用了Locate。几乎,因为您所包含的loPartialKey选项在搜索TDateTime值时是没有意义的。在这种情况下,您需要搜索确切的日期、时间值。问题在你的测试中。
您的第一次测试有错误的日期时间值。它的毫秒部分在您的转换中被忽略了,所以您实际上正在尝试定位日期时间26/10/2015 17:13:36,它不在您的表中。
在第二种情况下,您试图定位日期时间26/10/2015 17:13,它不在您的表中。
我建议使用例如EncodeDateTime函数来构建日期时间,而不是字符串转换,并使用loPartialKey选项删除这些额外的调用。
发布于 2019-12-18 10:14:53
使用类型字符串来定位或使用yyyy dd格式的日期位置。
示例:
在查找:
if tblLog.Locate('USERDATE', '2015-10-26', []) then ...在其中:
select * from tbllog where userdate = '2015-10-26'https://stackoverflow.com/questions/48582468
复制相似问题