请教:vb如何读取Win2000 Server服务器日期与时间
解决方案 »
- 获取一款游戏的验证码
- 为什么有些用vb设计的程序(已生成.exe)脱离vb 环境便打不开?
- 有人用过KODICOM8800的视频采集卡吗?
- 窗体如何与webBrowse控件里的Web页面交互,比较有难度....大分酬谢
- 请问一下这种写法是什么意思?例如:ReDim m_Order(1 To 1) As Integer
- 怎么在vb 玻璃效果
- 请问几个 关于数学的 函数
- 申奥、十强、入世、中国人!!!
- 怎样通过代码设置数据环境的数据源?
- 在VB中如何制作在一窗口按钮CLICKED后,弹出一菜单风格为WIN95开始菜单风格
- 我想让VB做的客户端上的时间与服务器上的时间同步,该怎么做?
- 如何获得应用程序所在的路径?
可以使用select getdate() as sj来得到
http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=194046
---------------------------------------------------------------
net time \\192.168.0.1
---------------------------------------------------------------
Public Function GetServerDateShell(ByVal sServer As String) As Date
If Left$(sServer, 2) <> "\\" Then sServer = "\\" & sServer
Dim nFile As Integer, t As Long
Dim sBatFile As String, sTextFile As String, sTemp As String
sBatFile = App.Path & "\nettime.bat"
sTextFile = App.Path & "\nettime.txt"
nFile = FreeFile
Open sBatFile For Output As #nFile
Print #nFile, "net time " & sServer & " > " & App.Path & "\nettime.txt"
Close #nFile
On Error Resume Next
Shell sBatFile, vbHide
If Err Then Exit Function
While Dir(sTextFile) = "" 'waiting file creation
Wend
Sleep 1000 ' wait while system write to file
Open sTextFile For Input As #nFile
Line Input #nFile, sTemp
Close #nFile
GetServerDateShell = CDate(Mid(sTemp, InStr(1, sTemp, ":") + 2))
Kill sBatFile
Kill sTextFile
End Function
''
' 读取服务器日期值<Br>
'
'
'@return 返回当前服务器的日期
Public Function ServerDate() As Date
On Error GoTo ErrHandle
Dim strSQL As String
Dim objRs As New Recordset
strSQL = "Select GetDate() as NowDate"
If Not conObject Is Nothing Then
Set objRs = conObject.Execute(strSQL)
ServerDate = objRs(0).Value
End If
Exit Function
ErrHandle:
err.Raise err.Number, err.Source, err.Description & vbCrLf & vbTab & "@DataAccess" & ".ServerDate", err.HelpFile, err.HelpContext
End Function我也没看明白
不知道管不管用
Private Type TIME_OF_DAY_INFO
tod_elapsed As Long
tod_msecs As Long
tod_hours As Long
tod_mins As Long
tod_secs As Long
tod_hunds As Long
tod_timezone As Long
tod_tinterval As Long
tod_day As Long
tod_month As Long
tod_year As Long
tod_weekday As Long
End Type'NetAPI Calls
Private Declare Function NetRemoteTOD Lib "netapi32.dll" (yServer As Any, pBuffer As Long) As Long
Private Declare Function NetApiBufferFree Lib "netapi32.dll" (ByVal pBuffer As Long) As Long
'Kernel API Calls
Private Declare Sub CopyMem Lib "kernel32.dll" Alias "RtlMoveMemory" (pTo As Any, uFrom As Any, ByVal lSize As Long)
'Return the Time and Date of a specified Machine on the Net
Public Function GetRemoteTime(ServerName As String) As Date
Dim lpBuffer As Long
Dim t_struct As TIME_OF_DAY_INFO
Dim ret As Long
Dim bServer() As Byte If Trim(ServerName) = "" Then
'Local machine
ret = NetRemoteTOD(vbNullString, lpBuffer)
Else
'Check the syntax of the ServerName string
If InStr(ServerName, "\\") = 1 Then
bServer = ServerName & vbNullChar
Else
bServer = "\\" & ServerName & vbNullChar
End If
ret = NetRemoteTOD(bServer(0), lpBuffer)
End If
CopyMem t_struct, ByVal lpBuffer, Len(t_struct)
If lpBuffer Then
Call NetApiBufferFree(lpBuffer)
End If
GetRemoteTime = DateSerial(t_struct.tod_year, t_struct.tod_month, t_struct.tod_day) + TimeSerial(t_struct.tod_hours, t_struct.tod_mins - t_struct.tod_timezone, t_struct.tod_secs)
End Function'Get the time and date of the local machine
Private Sub Command1_Click()
MsgBox GetRemoteTime("")
End Sub'Get the time and date a remote Workstation
Private Sub Command2_Click()
MsgBox GetRemoteTime("SERVER") '服務器名稱
End Sub
Private Declare Function NetRemoteTOD Lib "Netapi32.dll" ( _
tServer As Any, pBuffer As Long) As Long
Private Type SYSTEMTIME
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End TypePrivate Type TIME_ZONE_INFORMATION
Bias As Long
StandardName(32) As Integer
StandardDate As SYSTEMTIME
StandardBias As Long
DaylightName(32) As Integer
DaylightDate As SYSTEMTIME
DaylightBias As Long
End TypePrivate Declare Function GetTimeZoneInformation Lib "kernel32" (lpTimeZoneInformation As TIME_ZONE_INFORMATION) As LongPrivate Declare Function NetApiBufferFree Lib "Netapi32.dll" (ByVal lpBuffer As Long) As Long'Private Type TIME_OF_DAY_INFO
tod_elapsedt As Long
tod_msecs As Long
tod_hours As Long
tod_mins As Long
tod_secs As Long
tod_hunds As Long
tod_timezone As Long
tod_tinterval As Long
tod_day As Long
tod_month As Long
tod_year As Long
tod_weekday As Long
End Type'Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Public Function getRemoteTOD(ByVal strServer As String) As Date
Dim result As Date
Dim lRet As Long
Dim tod As TIME_OF_DAY_INFO
Dim lpbuff As Long
Dim tServer() As Byte
tServer = strServer & vbNullChar
lRet = NetRemoteTOD(tServer(0), lpbuff) If lRet = 0 Then
CopyMemory tod, ByVal lpbuff, Len(tod)
NetApiBufferFree lpbuff
result = DateSerial(tod.tod_year, tod.tod_month, tod.tod_day) + _
TimeSerial(tod.tod_hours, tod.tod_mins - tod.tod_timezone, tod.tod_secs)
getRemoteTOD = result
Else
Err.Raise Number:=vbObjectError + 1001, _
Description:="cannot get remote TOD"
End If
End Function'要运行该程序,通过如下方式调用。
Private Sub Command1_Click()
Dim d As Date
d = getRemoteTOD("\\Win2000ServerMachineName")
MsgBox d
End Sub
Conn为连接.
'得到服务器系统时间
Dim rst As ADODB.Recordset
Set rst = New ADODB.Recordset
rst.Open "select getdate() as sj", Conn, adOpenDynamic, adLockOptimistic
Time = rst.Fields("sj")
rst.Close
方法:在服务器上创建一个空文件,然后取文件的时间。最后删除。
Public Function GetServerDateTrick(ByVal sServer As String) As Date
Dim sTempFile As String
If Left$(sServer, 2) <> "\\" Then sServer = "\\" & sServer
If Right$(sServer, 1) <> "\" Then sServer = sServer & "\"
sTempFile = sServer & "c\time.tmp"
On Error Resume Next
Open sTempFile For Binary As #1
Close #1
GetServerDateTrick = FileDateTime(sTempFile)
If Err Then GetServerDateTrick = 0
Kill sTempFile
End Function