请教:vb如何读取Win2000 Server服务器日期与时间

解决方案 »

  1.   

    如果有sqlserver
    可以使用select getdate() as sj来得到
      

  2.   

    参考,使用文件获得
    http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=194046
      

  3.   

    比如,通过ip地址,或计算机名,用vb代码来获取时间  
    ---------------------------------------------------------------  
     
    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  
      

  4.   


    ''
    ' 读取服务器日期值<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我也没看明白
    不知道管不管用
      

  5.   

    不能運行在98下面Option Explicit'API Structures
    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
      

  6.   

    作1个socket服务器客户端,服务器用udp定时给客户端较时,
      

  7.   

    Option Explicit
    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
      

  8.   

    窗体上放一Timer控件,在其Timer事件下写入如下代码:
    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
      

  9.   

    另类的解决方法。要求你要有服务器写的操作权限。
    方法:在服务器上创建一个空文件,然后取文件的时间。最后删除。
    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