主要需求是因為 有些 Client 端 PC 都是服役多年的老 PC 所以 BIOS 電池都沒力了,又懶得一台一台換。
所以直接從 SQL Server 端 作時間的校正。
[DllImport("Kernel32.dll")]
private extern static uint SetLocalTime(ref SYSTEMTIME lpSystemTime);
[StructLayout(LayoutKind.Sequential)]
private 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 wMilliseconds;
}
public void SetTime(DateTime SqlServerTime)
{
SYSTEMTIME st = new SYSTEMTIME();
st.wYear = Convert.ToUInt16(SqlServerTime.Year);
st.wMonth = Convert.ToUInt16(SqlServerTime.Month);
st.wDay = Convert.ToUInt16(SqlServerTime.Day);
st.wHour = Convert.ToUInt16(SqlServerTime.Hour);
st.wMilliseconds = Convert.ToUInt16(SqlServerTime.Millisecond);
st.wMinute = Convert.ToUInt16(SqlServerTime.Minute);
st.wSecond = Convert.ToUInt16(SqlServerTime.Second);
SetLocalTime(ref st);
}
//使用時
ChangeLocalDateTime CLDT = new ChangeLocalDateTime();
CLDT.SetTime(db_dt); //db_dt 是指從 SQL Server 取回來的 GETDATE()
沒有留言:
張貼留言