|
- [DllImportAttribute("kernel32.dll")]
- public static extern void GetLocalTime(ref SystemTime st);
- [DllImportAttribute("kernel32.dll")]
- public static extern void SetLocalTime(ref SystemTime st);
复制代码
- string sql = "SELECT Getdate()";
- string sTime = TransactSql(destinationConn, sql);
- DateTime dt = new DateTime();
- DateTime.TryParse(sTime,out dt);
- Console.WriteLine("设置系统时间为:" + dt.ToLongDateString() + " " + dt.ToLongTimeString());
- SystemTime st = new SystemTime();
- st.vYear = (ushort)dt.Year;
- st.vMonth = (ushort)dt.Month;
- st.vDay = (ushort)dt.Day;
- st.vDayOfWeek = (ushort)dt.DayOfWeek;
- st.vHour = (ushort)dt.Hour;
- st.vMinute = (ushort)dt.Minute;
- st.vSecond = (ushort)dt.Second;
- st.vMiliseconds = (ushort)dt.Millisecond; ;
- SetLocalTime(ref st);
复制代码
- [StructLayout(LayoutKind.Sequential)]
- public struct SystemTime
- {
- public ushort vYear;
- public ushort vMonth;
- public ushort vDayOfWeek;
- public ushort vDay;
- public ushort vHour;
- public ushort vMinute;
- public ushort vSecond;
- public ushort vMiliseconds;
- }
复制代码
- /// <summary>
- /// 返回第一行第一列的字符串数据
- /// </summary>
- /// <param name="conn"></param>
- /// <param name="sql"></param>
- /// <returns></returns>
- public static string TransactSql(SqlConnection conn, string sql)
- {
- if (conn.State != System.Data.ConnectionState.Open)
- {
- conn.Open();
- }
- SqlCommand cmd = new SqlCommand(sql, conn);
- try
- {
- string rt = cmd.ExecuteScalar().ToString();
- return rt;
- }
- catch (SqlException ex)
- {
- throw new Exception(ex.Message);
- }
- }
复制代码 |
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|