下面是我的一段程序,不知道是什么原因rst.Recordcount=-1.
'保存ID号和时间
'1.连接数据库
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim strcnn, SQL As String
Dim i As Integer
Dim CurrentDBTime As Date
TempTime = 10
MousePointer = vbHourglass
strcnn = "Provider=SQLOLEDB.1;Persist Security Info=True;User ID=Sa;Password=;Initial Catalog=rfid;Data Source=front"
Set cnn = New ADODB.Connection
Set rst = New ADODB.Recordset
cnn.Open strcnn
MousePointer = vbDefault
SQL = "Select * From memberinfo "
cnn.Execute (SQL)
rst.Open SQL, cnn, adOpenDynamic, adLockBatchOptimistic
'2访问数据库
If rst.EOF = False Then
For i = 0 To rst.RecordCount
CurrentID = rst.Fields("bh").Value
CurrentDBTime = rst.Fields("qdsj").Value
If Communication.GetCurrentTagid(nCounter) = CurrentID And DateDiff("s", Trim(CurrentDBTime), CurrentSysTime) >= TempTime Then
rst.AddNew
rst.Fields("bh") = Communication.GetCurrentTagid(nCounter)
rst.Fields("qdsj") = CurrentSysTime
rst.Update
rst.MoveFirst
End If
Next i
End If
'保存ID号和时间
'1.连接数据库
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim strcnn, SQL As String
Dim i As Integer
Dim CurrentDBTime As Date
TempTime = 10
MousePointer = vbHourglass
strcnn = "Provider=SQLOLEDB.1;Persist Security Info=True;User ID=Sa;Password=;Initial Catalog=rfid;Data Source=front"
Set cnn = New ADODB.Connection
Set rst = New ADODB.Recordset
cnn.Open strcnn
MousePointer = vbDefault
SQL = "Select * From memberinfo "
cnn.Execute (SQL)
rst.Open SQL, cnn, adOpenDynamic, adLockBatchOptimistic
'2访问数据库
If rst.EOF = False Then
For i = 0 To rst.RecordCount
CurrentID = rst.Fields("bh").Value
CurrentDBTime = rst.Fields("qdsj").Value
If Communication.GetCurrentTagid(nCounter) = CurrentID And DateDiff("s", Trim(CurrentDBTime), CurrentSysTime) >= TempTime Then
rst.AddNew
rst.Fields("bh") = Communication.GetCurrentTagid(nCounter)
rst.Fields("qdsj") = CurrentSysTime
rst.Update
rst.MoveFirst
End If
Next i
End If
rst.CursorLocation = adUseClient
rst.open ..............................
msgbox rst.RecordCount
Set rst = New ADODB.Recordset
cnn.CursorLocation = adUseClient
cnn.Open strcnn
......
或者作循环的时候:
do while not Rst.eof
rst.movenext
loop
把 rst.Open SQL, cnn, adOpenDynamic, adLockBatchOptimistic
的adOpenDynamic改成OpenKeyset就可以了