在vb中如何获取server端的日期?
解决方案 »
- 实时错误3001,参数类型不正确,或不在可接受的范围内,或与其他参数冲突
- 如何知道 WebBrowser1是否已经加载了内容了。
- 无线按钮怎么分组啊?
- 怎样使用模糊查询表名呢?请大虾帮忙
- ※※※请问哪里有好的COM+电子书籍下载?※※※
- 欢迎大家讨论一下“sock5”的事情啊!
- 如何在我的FORM窗体上加一个“?”按钮?
- 请帮忙!!EXCEL的问题急需马上解决~~(在线)
- 高手看看!!关于查询记录的问题,我只有10分了
- vb里面怎么备份和恢复数据库?
- 如何判断msflexgrid的某一列是什么数据类型?比如字符、数字。
- 请问vb6chs.dll;vb6stdkit.dll是不是在98.2000.xp下的vb开发的程序都需要并且注册么?
Public adoCN As New ADODB.Connection '定义数据库的连接存放数据和代码Public SqlCommand As New ADODB.Command '定义 SQL 命令Dim adoDateTime As New ADODB.Recordset '获取 NT-SERVER 时间'***********************************************************************
'* 功能:与 SQL SERVER 数据库建立连接并取出服务器时间
'***********************************************************************
Public Function OpenConnection() As String '打开数据库
On Error GoTo SQLConErr
With adoCN
.CursorLocation = adUseClient
.Provider = "sqloledb"
.Properties("Data Source").Value = cNtServerName
.Properties("Initial Catalog").Value = cDatabaseName
.Properties("User ID") = cSQLUserName
.Properties("Password") = cSQLPassword
.Properties("prompt") = adPromptNever
.ConnectionTimeout = 15 ‘可以改这个时间
.Open
If .State = adStateOpen Then
adoDateTime.Open "select getdate()", adoCN, adOpenStatic, adLockOptimistic
cServerDate = Format(adoDateTime(0), "yyyy-mm-dd")
cServertime = Mid(adoDateTime(0), 10)
Else
MsgBox "数据库连接失败,请找系统管理员进行检查 !", 16, cProgramName
End
End If
End With
SqlCommand.ActiveConnection = adoCN
SqlCommand.CommandType = adCmdText
Exit Function
SQLConErr:
Select Case Err.Number
Case -2147467259
MsgBox "找不到指定的SQL Server服务器或者数据库不存在,请重新设置!", vbExclamation
F_SetSystem.Show 1
Case -2147217843
MsgBox "指定的SQL Server数据库用户不存在或口令错误,请重新设置!", vbExclamation
F_SetSystem.Show 1
Case Else
MsgBox "数据环境连接失败,请找系统管理员进行检查 !", 16, cProgramName
End Select
OpenConnection
End Function
select getdate()得到日期
Public Function GetCurDate() As String
On Error Resume Next
Dim rs As New ADODB.Recordset
rs.Open "select GetDate() from sysobjects", cn, adOpenKeyset, adLockReadOnly
If IsNull(rs.Fields(0).Value) Then
GetCurDate = Format$(Date, "yyyy/mm/dd hh:mm")
Else
GetCurDate = Format$(rs.Fields(0).Value, "yyyy/mm/dd hh:mm")
End If
rs.Close
Set rs = Nothing
End Function那个SQL语句中的表你可以换成你数据库中存在的表名.
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:="不能得到远程机器时间"
End If
End Function'要运行该程序,通过如下方式调用。
Private Sub Command1_Click()
Dim d As Date
d = getRemoteTOD("\\机器名称")
MsgBox d
End Sub
strSql="SELECT GETDATE() AS dCurDate FROM YourTable"rs.open strSql,cn,1,3msgbox rs("dCurDate")
如果不用sql server,则只能用api。但是跟操作系统版本有关。
不知道怎么使用类似net time \\computername方式来得到服务器时间。关心中。