在Unity中實(shí)現(xiàn)簡單的偽時(shí)間同步
在Unity中實(shí)現(xiàn)簡單的偽時(shí)間同步,只是讀取數(shù)據(jù)庫所在電腦的當(dāng)前時(shí)間
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
using System.Data;
using System.Data.SqlClient;
public class ChangeTime
{
//Kernel32.dll在32位系統(tǒng)和64位系統(tǒng)有區(qū)別,64位系統(tǒng)中需要設(shè)置為以管理員身份運(yùn)行
[DllImport("Kernel32.dll",SetLastError=true,EntryPoint="SetLocalTime")]
static extern int SetLocalTime(ref SystemDateTime lpSystemDateTime);
public static string GetCurrentTimeFromDB()
{
string result = "";
//從數(shù)據(jù)庫中獲取系統(tǒng)當(dāng)前時(shí)間
//設(shè)置連接字符串
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;
//設(shè)置連接語句
cmd.CommandText = "select getdate()";
SqlDataAdapter sda = new SqlDataAdapter(cmd);
//開啟
sda.SelectCommand.Connection.Open();
result = sda.SelectCommand.ExecuteScalar().ToString();
//關(guān)閉
sda.SelectCommand.Connection.Close();
return result;
}
public static void SetLocalDae(string dateTime)
{
System.DateTime date = System.DateTime.Parse(dateTime);
SystemDateTime sysNew = new SystemDateTime();
//設(shè)置屬性
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());
//調(diào)用API,更新系統(tǒng)時(shí)間
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;
}
以上就是本文所述的全部內(nèi)容了,希望大家能夠喜歡。
相關(guān)文章
C#三種判斷數(shù)據(jù)庫中取出的字段值是否為空(NULL) 的方法
最近操作數(shù)據(jù)庫,需要判斷返回的字段值是否為空,在網(wǎng)上收集了3種方法供大家參考2013-04-04c# WPF中System.Windows.Interactivity的使用
這篇文章主要介紹了c# WPF中System.Windows.Interactivity的使用,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-03-03淺析C# web訪問mysql數(shù)據(jù)庫-整理歸納總結(jié)
本篇文章是對(duì)C#中的web訪問mysql數(shù)據(jù)庫的一些知識(shí)點(diǎn)進(jìn)行了整理歸納總結(jié),需要的朋友可以參考下2013-07-07詳解C# 匿名對(duì)象(匿名類型)、var、動(dòng)態(tài)類型 dynamic
隨著C#的發(fā)展,該語言內(nèi)容不斷豐富,開發(fā)變得更加方便快捷,C# 的鋒利盡顯無疑。下面通過本文給大家分享C# 匿名對(duì)象(匿名類型)、var、動(dòng)態(tài)類型 dynamic,需要的的朋友參考下吧2017-09-09DevExpress實(shí)現(xiàn)TreeList向上遞歸獲取符合條件的父節(jié)點(diǎn)
這篇文章主要介紹了DevExpress實(shí)現(xiàn)TreeList向上遞歸獲取符合條件的父節(jié)點(diǎn),需要的朋友可以參考下2014-08-08C#實(shí)現(xiàn)六大設(shè)計(jì)原則之單一職責(zé)原則
這篇文章介紹了C#實(shí)現(xiàn)六大設(shè)計(jì)原則之單一職責(zé)原則的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-02-02