C # synchronize the Time of the workstation and SQL Server and set the local time

Source: Internet
Author: User

This article reprinted: http://www.csframework.com/archive/2/arc-2-20130101-1971.htm

 

C # code:

///   <Summary>  
/// Set local time
///   </Summary>  
Public Class Localtimesync
{
[Dllimport ( " Kernel32.dll " )]
Public Static Extern Bool Setsystemtime ( Ref Systemtime required IME );

[Dllimport ( " Kernel32.dll " )]
Public Static Extern Void Getsystemtime ( Ref Systemtime required IME );

[Structlayout (layoutkind. Sequential)]
Public Struct Systemtime
{
Public Ushort Wyear;
Public Ushort Wmonth;
Public Ushort Wdayofweek;
Public Ushort Wday;
Public Ushort Whour;
Public Ushort Wminute;
Public Ushort Wsecond;
Public Ushort Wmiliseconds;
}
///   <Summary>  
/// Set local time
///   </Summary>  
Public Static Void Synctime (datetime currenttime)
{
Systemtime required IME = New Systemtime ();
Required ime. wyear = convert. touint16 (currenttime. year );
Required ime. wmonth = convert. touint16 (currenttime. month );
Required ime. wday = convert. touint16 (currenttime. Day );
Systime. wdayofweek = convert. touint16 (currenttime. dayofweek );
Systime. wminute = convert. touint16 (currenttime. Minute );
Systime. wsecond = convert. touint16 (currenttime. Second );
Systime. wmiliseconds = convert. touint16 (currenttime. millisecond );

// Processing Beijing Time
Int Nbeijinghour = currenttime. Hour- 8 ;
If (Nbeijinghour <= 0 )
{
Nbeijinghour = 24 ;
Required ime. wday = convert. touint16 (currenttime. Day- 1 );
// Systime. wdayofweek = convert. touint16 (current. dayofweek-1 );
}
Else
{
Required ime. wday = convert. touint16 (currenttime. Day );
Systime. wdayofweek = convert. touint16 (currenttime. dayofweek );
}
Systime. whour = convert. touint16 (nbeijinghour );

Setsystemtime ( Ref Systime ); // Set local time
}
}

//Source: C/S framework Network (www.csframework.com) QQ: 1980854898

 
C # code:

///   <Summary>  
/// Sync server time
///   </Summary>  
Public Void Syncservertime ()
{
String SQL = " Select getdate () " ;
Object O = dataprovider. instance. executescalar (globals. def_attend_system_db, SQL );
Datetime dt = convertex. todatetimeex (O );
// Localtimesync. synctime (DT );

// Comparison Time Consistency
If (Convertex. tocharyyyymmddhhmm (datetime. Now )! = Convertex. tocharyyyymmddhhmm (DT ))
Localtimesync. synctime (DT ); // Synchronization server time
}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.