VB 如何获得SQL server 服务器的日期时间?

解决方案 »

  1.   

    你说清楚呀,你执行的服务器和SQL是一个服务器吗?如果是now()
      

  2.   

    调用存储过程
    create prop sjsp
    select getdate() as sjvb
    rs.open "sjsp",conn
    text1.text=rs.field("sj")
      

  3.   

    李大虾的方法参SQL下试没错,可是要是在本地机上得到服务器的时间如何做呀,我是没试出来.
      

  4.   

    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
      

  5.   

    Public Function Gettime() As Date  '取服务器时间
        Dim rs As New ADODB.Recordset
        on error goto err
        Set rs = "select getdate() as dd", CN, adOpenKeyset, adLockOptimistic, adCmdText
        If rs.RecordCount > 0 Then Gettime = rs.Fields(0).Value
        err:
    If Not rs Is Nothing Then
        If rs.State <> adStateClosed Then
            rs.Close
            Set rs = Nothing
        Else
            Set rs = Nothing
        End If
    End If
    End Function
      

  6.   

    哪有那么麻烦啊,呵。
    正确方法是:
    建一个Adodc1
    设好SQL数据源(SQL数据源会设吧,别说不会哦,呵),在Adodc1的记录源(SQL文本中)写:“select getdate() as MYTIME”
    然后建一个text1,把text1的数据源设为Adodc1,字段设为MYTIME,然后就可以了……
    要使它不停地走,加上timer控件并不停刷新就好了……