因为我的电脑时钟坏了,所以总是不知道时间,做一个小程序,如下:Private Sub Timer1_Timer()
Label2.Caption = Format(Time, "hh:mm:ss")
End Sub最简单,实现取电脑上的数据,现想取其它电脑如服务器上的数据,因为服务器年数据正确!
怎么办呢!
Label2.Caption = Format(Time, "hh:mm:ss")
End Sub最简单,实现取电脑上的数据,现想取其它电脑如服务器上的数据,因为服务器年数据正确!
怎么办呢!
select getdate()
返回SQL服务上的时间
'ADO连接
Public Cn As ADODB.ConnectionPrivate Sub Form_Load()
DBConnection
labTime.Caption = GetDateTime
End Sub'========================================================================================
'=日期时间形式取当前DataServer时间
'========================================================================================
Public Function GetDateTime() As Date
On Error GoTo ErrH
GetDateTime = GetFiledValue("SELECT GETDATE()")
Exit Function
ErrH:
Err.Clear
GetDateTime = ""
Exit Function
End Function'========================================================================================
'=数据连接
'========================================================================================
Public Function DBConnection() As Boolean
On Error GoTo ErrH
DBConnection = True
StrConn = "Provider=SQLOLEDB.1;Password=密码;Persist Security Info=True;User ID=sa;Initial Catalog=master;Data Source=服务器"
'-检测是否有连接
If Cn Is Nothing Then
Set Cn = New ADODB.Connection
End If
'如果连接断开或未连接,则开始连接
If Cn.State = 0 Then Cn.Open StrConn
Cn.CommandTimeout = 600
DBConnection = False
Exit Function
ErrH:
MsgBox Err.Description, vbCritical
Err.Clear
DBConnection = True
Exit Function
End Function'========================================================================================
'=得到SQL返回的第一行,第一列的数据.忽略其它行、列的数据
'========================================================================================
Function GetFiledValue(sSQL As String) As String
Dim AdoRs As ADODB.Recordset
On Error GoTo ErrH
Set AdoRs = GetRecordset(sSQL)
With AdoRs
If ChkRsState(.Clone) Then
GetFiledValue = ""
Else
.MoveFirst
GetFiledValue = CStr(.Fields(0))
End If
End With
Set AdoRs = Nothing
Exit Function
ErrH:
GetFiledValue = ""
Set AdoRs = Nothing
Err.Clear
Exit Function
End Function
If DBConnection Then Exit Sub
labTime.Caption = GetDateTime
End Sub
On Error GoTo hError
Dim todOut As TIME_OF_DAY_INFO
Dim tmSystem As SYSTEMTIME
Dim tmLocal As FILETIME
Dim tmFileTime As FILETIME
Dim byteArr(1 To 48) As Byte 'because len(todout)=48
Dim todIn As Long 'a point to get server time
Dim lRet As Long
Dim strTrySvr As String
Dim strReturn As String fun_GetServerTime = ""
todIn = 0
If strSvr = "" Then
lRet = NetRemoteTOD(0, todIn)
If lRet <> 0 Then Exit Function 'error
Call CopyMemory(byteArr(1), ByVal todIn, 48)
Else
strSvr = UCase(strSvr)
strTrySvr = StrConv(strSvr, vbUnicode)
lRet = NetRemoteTOD(ByVal strTrySvr, todIn)
If lRet = 0 Then
Call CopyMemory(byteArr(1), ByVal todIn, 48)
Else 'if win200 server must be : "\\" + servername
If Left$(strSvr, 2) = "\\" Then Exit Function
strTrySvr = "\\" & strSvr
strTrySvr = StrConv(strTrySvr, vbUnicode)
lRet = NetRemoteTOD(ByVal strTrySvr, todIn)
If lRet <> 0 Then Exit Function 'error
Call CopyMemory(byteArr(1), ByVal todIn, 48)
End If
End If
If todIn <> 0 Then NetApiBufferFree (todIn)
CopyMemory todOut, byteArr(1), 48
tmSystem.wDay = todOut.tod_day
tmSystem.wDayOfWeek = todOut.tod_weekday
tmSystem.wHour = todOut.tod_hours
tmSystem.wMinute = todOut.tod_mins
tmSystem.wMonth = todOut.tod_month
tmSystem.wSecond = todOut.tod_secs
tmSystem.wYear = todOut.tod_year
lRet = SystemTimeToFileTime(tmSystem, tmFileTime)
If lRet = 0 Then Exit Function 'error
lRet = FileTimeToLocalFileTime(tmFileTime, tmLocal)
If lRet = 0 Then Exit Function 'error
lRet = FileTimeToSystemTime(tmLocal, tmSystem)
If lRet = 0 Then Exit Function 'error
strReturn = tmSystem.wYear & "-" & tmSystem.wMonth & "-" & tmSystem.wDay & " " & _
tmSystem.wHour & ":" & tmSystem.wMinute & ":" & tmSystem.wSecond
fun_GetServerTime = Format$(strReturn, "YYYY年MM月DD日 HH:MM:SS")
Exit Function
hError:
fun_GetServerTime = ""
End Function
简单点,先用net time使时间同步,然后读本机时间就可以了
Net time使计算机的时钟与另一个计算机或域的时钟同步。使用不带参数的 net time 显示另一台计算机或域的时间。语法
net time [{\\ComputerName | /domain[:DomainName] | /rtsdomain[:DomainName]}] [/set]net time [\\ComputerName] [/querysntp] [/setsntp[:NTPServerList]]参数
\\computername
指定要检查或与之同步的服务器的名称。
/domain[:domainname]
指定要同步时钟的域。
/rtsdomain[:domainname]
指定要与之同步时钟的“可信时间服务器”所在的域。
/set
使计算机的时钟与指定的计算机或域的时间同步。
/querysntp
显示当前为本地计算机或 \\computername 所指定的计算机配置的网络时间协议 (NTP) 服务器的名称。
/setsntp[:NTPServerList]
指定本地计算机所使用的 NTP 时间服务器的列表。该列表可以包含 IP 地址 或 DNS 名称,并用空格分开。如果使用多个时间服务器,则必须使用引号引住该列表。
net help command
显示指定 net 命令的帮助。
格式化图例
格式 含义
斜体 用户必须提供的信息
粗体 用户必须准确键入的要显示的元素
省略号 (...) 在命令行中可多次重复的参数
中括号 ([]) 可选项
大括号 ({});选项用竖线 (|) 分隔。例如: {even|odd} 用户必须从选项集合中选择一个
Courier 字体 代码或程序输出
相关主题