指示ADODB Recordset使用SQL Server当前时间戳

时间:2012-11-19 12:01:01

标签: sql-server-2008 vb6 ado recordset

我正在尝试使用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

1 个答案:

答案 0 :(得分:1)

您可以使用TSQL GetDate() Function

将您的选择更改为:

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