Classic ASP Do while Loop 只显示最后一条记录
Classic ASP Do while Loop only shows the last record
我有一个包含 3 条记录的记录集。我在经典 ASP 中使用 DO While 循环来显示所有记录,但下面的代码只显示记录集中的最后一条记录。
<%Do While Not RS.EOF%>
<div><%=RS.Fields("ID").value%></div>
<div><%=RS.Fields("Title").value%></div>
<div><%=RS.Fields("Description").value%></div>
<%RS.MoveNext()
Loop%>
谁能告诉我为什么会这样?与我填充记录集的方式有关吗?
我确定它包含 3 条记录 RS.RecordCount returns 3.
这是我用来创建虚拟记录集并用数据填充它的代码:
With RS.Fields
.Append "ID", adBSTR
.Append "Title", adBSTR
.Append "Description", adBSTR
End With
With RS
.AddNew
.Fields("ID")="1111"
.Fields("Title") = "Test1"
.Fields("Description") = "Test Description 1"
.Update
End With
With RS
.AddNew
.Fields("ID")="2222"
.Fields("Title") = "Test2"
.Fields("Description") = "Test Description 2"
.Update
End With
With RS
.AddNew
.Fields("ID")="3333"
.Fields("Title") = "Test3"
.Fields("Description") = "Test Description 3"
.Update
End With
After you call the AddNew method, the new record becomes the current
record and remains current after you call the Update method. Since the
new record is appended to the Recordset, a call to MoveNext following
the Update will move past the end of the Recordset, making EOF True.
这就是它发生的原因。
添加一条记录后需要列出记录集中的所有记录时,需要确保光标在开头。
为此,请在循环 Do While Not RS.EOF
开始之前调用 MoveFirst (RS.MoveFirst
)。
我有一个包含 3 条记录的记录集。我在经典 ASP 中使用 DO While 循环来显示所有记录,但下面的代码只显示记录集中的最后一条记录。
<%Do While Not RS.EOF%>
<div><%=RS.Fields("ID").value%></div>
<div><%=RS.Fields("Title").value%></div>
<div><%=RS.Fields("Description").value%></div>
<%RS.MoveNext()
Loop%>
谁能告诉我为什么会这样?与我填充记录集的方式有关吗?
我确定它包含 3 条记录 RS.RecordCount returns 3.
这是我用来创建虚拟记录集并用数据填充它的代码:
With RS.Fields
.Append "ID", adBSTR
.Append "Title", adBSTR
.Append "Description", adBSTR
End With
With RS
.AddNew
.Fields("ID")="1111"
.Fields("Title") = "Test1"
.Fields("Description") = "Test Description 1"
.Update
End With
With RS
.AddNew
.Fields("ID")="2222"
.Fields("Title") = "Test2"
.Fields("Description") = "Test Description 2"
.Update
End With
With RS
.AddNew
.Fields("ID")="3333"
.Fields("Title") = "Test3"
.Fields("Description") = "Test Description 3"
.Update
End With
After you call the AddNew method, the new record becomes the current record and remains current after you call the Update method. Since the new record is appended to the Recordset, a call to MoveNext following the Update will move past the end of the Recordset, making EOF True.
这就是它发生的原因。
添加一条记录后需要列出记录集中的所有记录时,需要确保光标在开头。
为此,请在循环 Do While Not RS.EOF
开始之前调用 MoveFirst (RS.MoveFirst
)。