需要在ASP中使用SQL显示多行

时间:2013-08-01 21:57:31

标签: sql asp-classic

我在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>

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

3 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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
%>

答案 2 :(得分: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的答案比我的好得多。