国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

在Unity中實現簡單的偽時間同步

2020-01-24 02:07:57
字體:
來源:轉載
供稿:網友

在Unity中實現簡單的偽時間同步,只是讀取數據庫所在電腦的當前時間

復制代碼 代碼如下:

using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
using System.Data;
using System.Data.SqlClient;
public class ChangeTime
{
    //Kernel32.dll在32位系統和64位系統有區別,64位系統中需要設置為以管理員身份運行
    [DllImport("Kernel32.dll",SetLastError=true,EntryPoint="SetLocalTime")]
    static extern int SetLocalTime(ref SystemDateTime lpSystemDateTime);
    public static string GetCurrentTimeFromDB()
    {
        string result = "";
        //從數據庫中獲取系統當前時間
        //設置連接字符串
        SqlConnection con = new SqlConnection ("Data Source=192.168.0.1;Initial Catalog=DB;User ID=sa;password=123456");
        SqlCommand cmd = new SqlCommand ();
        cmd.Connection = con;
        cmd.CommandType = System.Data.CommandType.Text;
        //設置連接語句
        cmd.CommandText = "select getdate()";
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        //開啟
        sda.SelectCommand.Connection.Open();
        result = sda.SelectCommand.ExecuteScalar().ToString();
        //關閉
        sda.SelectCommand.Connection.Close();
        return result;
    }
    public static void SetLocalDae(string dateTime)
    {
        System.DateTime date = System.DateTime.Parse(dateTime);
        SystemDateTime sysNew = new SystemDateTime();
        //設置屬性
        sysNew.tYear = short.Parse(date.Year.ToString());
        sysNew.tMonth = short.Parse(date.Month.ToString());
        sysNew.tDay = short.Parse(date.Day.ToString());
        sysNew.tHour = short.Parse(date.Hour.ToString());
        sysNew.tMinute = short.Parse(date.Minute.ToString());
        sysNew.tSecond = short.Parse(date.Second.ToString());
        //調用API,更新系統時間
        SetLocalTime(ref sysNew);
    }
}
/// <summary>
/// 定義變量用于接收
/// </summary>
public class SystemDateTime
{
    public short tYear;
    public short tMonth;
    public short tDayOfWeek;
    public short tDay;
    public short tHour;
    public short tMinute;
    public short tSecond;
    public short tMilliseconds;
}

以上就是本文所述的全部內容了,希望大家能夠喜歡。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 安西县| 巫溪县| 邹城市| 新泰市| 永兴县| 武义县| 东兰县| 丰城市| 奉节县| 通榆县| 星子县| 韶关市| 奉节县| 巢湖市| 无锡市| 新竹市| 攀枝花市| 梧州市| 镇坪县| 大庆市| 海口市| 石景山区| 皋兰县| 沭阳县| 顺平县| 长乐市| 恩平市| 蓝田县| 灵川县| 桑日县| 南丹县| 永年县| 佛山市| 开鲁县| 平远县| 顺昌县| 龙胜| 勃利县| 利津县| 威宁| 长武县|