一个c/s系统怎么在c端获得s端机器的时间。并把这个时间设置为c端的系统时间。
为什么我用以下代码得到的时间不是服务端的系统时间。是不是s端数据库时间并不等于s端的系统时间?S端是MSSQL.function GetServerDateTime: string;
var
aqSys: TADOQuery;
begin
result := '';
aqSys := TADOQuery.Create(nil);
try
aqSys.Connection :=form1.ADOConnection1;
aqSys.SQL.Text := 'SELECT GETDATE() as sd';
try
aqSys.Open;
result := FormatDateTime('yyyy-mm-dd hh:nn:ss', aqSys.FieldByName('sd').AsDateTime);
except
raise;
end;
finally
FreeAndNil(aqSys);
end;
end;procedure TForm1.BitBtn1Click(Sender: TObject);
var
SystemTime: TSystemTime;
begin
if GetServerDateTime = '' then Exit;
DateTimeToSystemTime(StrToDatetime(GetServerDateTime), SystemTime);
SetSystemTime(SystemTime);
end;
为什么我用以下代码得到的时间不是服务端的系统时间。是不是s端数据库时间并不等于s端的系统时间?S端是MSSQL.function GetServerDateTime: string;
var
aqSys: TADOQuery;
begin
result := '';
aqSys := TADOQuery.Create(nil);
try
aqSys.Connection :=form1.ADOConnection1;
aqSys.SQL.Text := 'SELECT GETDATE() as sd';
try
aqSys.Open;
result := FormatDateTime('yyyy-mm-dd hh:nn:ss', aqSys.FieldByName('sd').AsDateTime);
except
raise;
end;
finally
FreeAndNil(aqSys);
end;
end;procedure TForm1.BitBtn1Click(Sender: TObject);
var
SystemTime: TSystemTime;
begin
if GetServerDateTime = '' then Exit;
DateTimeToSystemTime(StrToDatetime(GetServerDateTime), SystemTime);
SetSystemTime(SystemTime);
end;
解决方案 »
- 两台通过adsl上网的电脑怎样进行通信?能提供点思路吗?
- 如何抓图,带鼠标
- 散分喽!CodeGear官方网站开通了,快去看看吧!
- 请教关于 Char PChar 的字符处理函数
- 别人给了我上百分,为什么我的可用分还是原来的没变!!!
- ★★★★★串口编程★★★★★
- 李维书中WebService例子,为啥客户端有时报:Invalid Content type text/html,SOAP expect text/xml
- 急,如何检测打印机是否联机!
- DEPHI设计一个餐饮桌台显示思路
- 为什么我的账号进不去了呢?我的账号是 Bob7946(X度空间),有近3000的可用分,2000多的参与分,340的专家分,但今天进不去了,总是报密码错,为什么呀?
- 高手请帮忙,我搞了两天也没搞定——关于用dbnavigator修改dbgrid数据的问题!
- 在delphi中怎样修改ip地址?
就看你下面的这个click对不对了.
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, ExtCtrls, FR_View, FR_Class;type
TTODInfo = record
ElapsedTime: Integer; {number of seconds since 00:00:00 January 1, 1970}
Milliseconds: Integer; {number of milliseconds since last system reset}
Hours: Integer; {current hour (0-23)}
Minutes: Integer; {current minute (0-59)}
Seconds: Integer; {current second (0-59)}
Hunds: Integer; {current hundredth of a second (0-99)}
TimeZone: Integer; {time against GMT in minutes}
{west of Greenwich gives positive, east negative values}
{value of -1 means undefined time zone}
Interval: Integer; {clock tick interval in ten-thousandth of a second (0.0001 s)}
Day: Integer; {day of the month (1-31)}
Month: Integer; {month of the year (1-12)}
Year: Integer; {year}
Weekday: Integer; {day of the week (0-6) 0 = Sunday, 1 = Monday etc.}
end;
PTODInfo = ^TTODInfo;type
TForm1 = class(TForm)
Label1: TLabel;
BitBtn1: TBitBtn;
procedure BitBtn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
const
netapi32 = 'netapi32.dll';function NetApiBufferFree(Buffer: Pointer): Integer; stdcall;
function NetRemoteTOD(UNCServerName: PWideChar; Info: Pointer): Integer; stdcall;function ServerTime(const UNCServer: string; var Stamp: TDateTime): Integer;implementation{$R *.dfm}function NetApiBufferFree; external netapi32 name 'NetApiBufferFree';
function NetRemoteTOD; external netapi32 name 'NetRemoteTOD';function ServerTime(const UNCServer: string; var Stamp: TDateTime): Integer;
var
ServerName: PWideChar;
tod: PTODInfo;
Year, Month, Day, Hour, Min, Sec, MSec: Word;
begin
GetMem(ServerName, (Length(UNCServer) + 1) * SizeOf(WideChar));
try
ServerName := StringToWideChar(UNCServer, ServerName, Length(UNCServer) + 1);
Result := NetRemoteTOD(ServerName, @tod);
if Result = 0 then
begin
try
Year := tod^.Year;
Month := tod^.Month;
Day := tod^.Day;
Hour := tod^.Hours;
Min := tod^.Minutes;
Sec := tod^.Seconds;
MSec := tod^.Hunds * 10;
if tod^.TimeZone = -1 then {undefined timezone}
Stamp := EncodeDate(Year, Month, Day) +
EncodeTime(Hour, Min, Sec, MSec)
else
Stamp := EncodeDate(Year, Month, Day) +
EncodeTime(Hour, Min, Sec, MSec) - (tod^.TimeZone / 1440);
finally
NetApiBufferFree(tod);
end;
end;
finally
FreeMem(ServerName);
end;
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
var
t: TDateTime;
begin
ServerTime('172.21.165.16', t);
label1.Caption := DateTimeToStr(t);
showmessage(formatdatetime('yyyy/mm/dd hh:nn:ss',t));end;end.