2

我的 ASP 文件开头有以下代码

<%  
Set rstest = Server.CreateObject("ADODB.Recordset")
sql = "SELECT * FROM Division;" 
rstest.Open sql, db
%>

在我拥有的同一个 ASP 的正文部分

<table width="200" border="1">
 <tr>
    <th>Date/Time</th>
    <th>Officer</th>
    <th>Comments</th>
  </tr>
 <tr>
    <td><% = Date_Field %></td>
    <td><% = First_Name %>&nbsp;<% = Last_Name %></td>
    <td><% = Comments %></td>
 </tr>
 <tr>
     <td><% = Date_Field %></td>
 <td><% = First_Name %>&nbsp;<% = Last_Name %></td>
 <td><% = Comments %></td>
 </tr>
 </table>

出于某种原因,我只看到一条重复记录,即使我的表中有五条唯一记录。为什么是这样?

4

3 回答 3

4

试试下面的代码。我认为这应该有效。

<%  
    Set rstest = Server.CreateObject("ADODB.Recordset")
    sql = "SELECT * FROM Division;" 
    rstest.Open sql, db
%>
    <table width="200" border="1">
        <tr>
            <th>Date/Time</th>
            <th>Officer</th>
            <th>Comments</th>
        </tr>
<%
    if rstest.EOF then
    response.write "No Records Found!"

    Do While NOT rstest.Eof
%>
    <tr>
        <td><% = Date_Field %></td>
        <td><% = First_Name %>&nbsp;<% = Last_Name %></td>
        <td><% = Comments %></td>
    </tr>
<%
    rstest.MoveNext()
    Loop
    End If
    rstest.Close
    Set rstest=nothing
    db.Close
    Set db=nothing
%>  
    </table>
于 2013-08-01T23:00:31.250 回答
1

谢谢!在四处寻找正确的解决方案后,这真的很有帮助。我喜欢你如何分解它,以便你可以将它移动到页眉、内容、页脚区域。

我有一个对我有用的细微变化。请注意,其中包含完整的连接字符串。此外,需要一点点挖掘的一件事是发现我们必须在表名周围加上方括号才能使其正常工作。

<% 
'declare the variables 
Dim Connection
Dim ConnString
Dim Recordset
Dim SQL

'define the connection string, specify database driver
ConnString="DRIVER={SQL Server};SERVER=localhost;UID=xxxx;" & _ 
"PWD=xxxx;DATABASE=xxxx"

'declare the SQL statement that will query the database
SQL = "SELECT * FROM [xxxx]"

'create an instance of the ADO connection and recordset objects
Set Connection = Server.CreateObject("ADODB.Connection")
Set Recordset = Server.CreateObject("ADODB.Recordset")

'Open the connection to the database
Connection.Open ConnString

'Open the recordset object executing the SQL statement and return records 
Recordset.Open SQL,Connection
%>

<%
'first of all determine whether there are any records 
If Recordset.EOF Then 
Response.Write("No Users Found.") 
Else 
'if there are records then loop through the fields 
Do While NOT Recordset.Eof  
%>


<div class="row">
    <div class="col-xs-4">USERNAME</div>
    <div class="col-xs-4">PASSWORD</div>
    <div class="col-xs-4">STATUS</div>
<% 
Response.write "<div class='col-xs-4'>"    
Response.write Recordset("userName")
Response.write "</div><div class='col-xs-4'>"    
Response.write Recordset("psword")
Response.write "</div><div class='col-xs-4'>"    
Response.write Recordset("stat")
Response.write "</div>"    
Recordset.MoveNext     
Loop
End If
%>
</div>

<%
'close the connection and recordset objects to free up resources
Recordset.Close
Set Recordset=nothing
Connection.Close
Set Connection=nothing
%>
于 2016-04-29T14:34:47.197 回答
0

您的代码将仅显示记录集中第一条记录的数据。您必须编写一个循环来获取其余部分,并在该代码中构建表格行。将这些表行放入单个变量中,然后使用该变量一次填充所有行,方式与下面的替代方法相同。

或者,您可以在 Sql 中构建表行:

<%  
Set rstest = Server.CreateObject("ADODB.Recordset")
sql = "SELECT '<tr><td>' + Date_Field + '</td><td>' + First_Name + ' ' + Last_Name + '</td><td>' + Comments + '</td></tr>' AS 'Officer_Rows' FROM Division;" 
rstest.Open sql, db
%>

并按以下方式检索整个行集:

<table width="200" border="1">
<tr>
    <th>Date/Time</th>
    <th>Officer</th>
    <th>Comments</th>
</tr>
<% = Officer_Rows %>
</table>

我应该指出,从数据库调用中获取 HTML 标记是一个非常糟糕的想法!@user704988 的回答比我的要好得多。

于 2013-08-01T23:26:57.907 回答