1

我正在尝试使用 ADO 从 VB6 应用程序将许多记录写回数据库。记录从数据库读入ADODB.Recordset并随后更新。我希望能够让集合中的每条记录使用数据库服务器的当前日期和时间,因为我不相信我们所有的机器都是时间同步的。有没有办法做到这一点而不必查询数据库CURRENT_TIMESTAMP(或类似的东西)?理想情况下,我希望有某种标志,我可以将字段值设置为,以便Recordset在数据库服务器上解决这个问题。

请参阅下面我的原型代码中的问题(在 while 循环中)。

Dim oConn As ADODB.Connection
Dim oRs As ADODB.Recordset
Dim sSQL As String
Dim sColumnName1 As String: sColumnName1 = "UserNID"
Dim sColumnName2 As String: sColumnName2 = "Username"
Dim sColumnName3 As String: sColumnName3 = "LastLoginDTM"

' Open a connection.
Set oConn = New ADODB.Connection
oConn.Open 'my connection string was here

' Make a query over the connection.
sSQL = "SELECT TOP 10 " & sColumnName1 & ", " & sColumnName2 & ", " & sColumnName3 & " " & _
       "FROM theTable"

Set oRs = New ADODB.Recordset
oRs.CursorLocation = adUseClient
oRs.Open sSQL, oConn, adOpenStatic, adLockBatchOptimistic, adCmdText
oRs.MoveFirst

While Not oRs.EOF
    oRs.Fields(sColumnName3).Value = '-=-=-=-=-What do I put here???-=-=-=-=-
    oRs.MoveNext
Wend

' Close the connection.
oConn.Close
Set oConn = Nothing
4

1 回答 1

1

您可以使用 TSQL GetDate() 函数

将您的选择更改为:

sSQL = "SELECT TOP 10 " & sColumnName1 & ", " & sColumnName2 & _ 
        ", " & sColumnName3 & ", GETDATE() AS ServerTimeStamp " & _
       "FROM theTable"

然后,您的代码可能是:

While Not oRs.EOF
    oRs.Fields(sColumnName3).Value = oRS.Fields("ServerTimeStamp").Value
    oRs.MoveNext
Wend
于 2012-11-19T14:12:15.103 回答