教您如何实现同步SQL Server日期时间

来源:互联网 发布:淘宝网首页杰奥思琳 编辑:程序博客网 时间:2024/05/22 02:22

SQL Server日期是很重要的,在我们使用SQL Server数据库的过程中,有时候会需要SQL Server服务器时间和本地系统时间保持同步,下面就为您介绍这种实现同步SQL Server日期时间的方法。

  1.  
  2. //同步SQL_Server服务器时间日期的过程  
  3. procedure TDM.SynchronizationSQLServerDateTime();  
  4. var  
  5.   TheServerDateTime:TDateTime;//定义服务器时间  
  6.   TheLocalDateTime:_SystemTime;//定义本地系统时间 引用 windows  
  7.   Year,Month,Day:Word;//定义年月日  
  8.   Hour,Min,Sec,MSec:Word;//定义时分秒毫秒  
  9. begin  
  10.   with CommonQuery do begin  
  11.        SQL.Clear;  
  12.        SQL.Add('select GetDate() as 服务器时间');  
  13.        Open;  
  14.        TheServerDateTime :FieldByName('服务器时间').AsDateTime;  
  15.   end;  
  16.   DecodeDate(TheServerDateTime,Year,Month,Day);//分解服务器年月日  
  17.   DecodeTime(TheServerDateTime,Hour,Min,Sec,MSec);//分解服务器时间  
  18.   //-------设定本地系统时间  
  19.   TheLocalDateTime.wYear:=Year;  
  20.   TheLocalDateTime.wMonth:=Month;  
  21.   TheLocalDateTime.wDay:=Day;  
  22.   TheLocalDateTime.wHour:=Hour;  
  23.   TheLocalDateTime.wMinute:=Min;  
  24.   TheLocalDateTime.wMilliseconds:=MSec;  
  25.   SetlocalTime(TheLocalDateTime);//更改本地系统时间    
  26. end;   

以上就是实现同步SQL Server日期时间的方法。

原创粉丝点击