在 ASP 中以表格形式显示记录






3.33/5 (6投票s)
2002年6月3日

160526

1502
使用 ASP 显示数据库记录的入门指南。
引言
以下代码将允许您在一个 HTML 表格中显示数据库表的内容。HTML 表格是在从数据库中提取记录时动态构建的。如果您复制并粘贴此代码,则只需进行少量更改 - 基本上只需更改 DSN、表和字段名称。请注意,此示例为了格式化目的包含一个内部表格,但这不是必需的。
代码
<%@ Language="VBScript%">
<html>
<head>
<title>Display all Records</title>
</head>
<body>
<%
strSQL = "Select * from TableName;" 'create SQL string (where clauses etc
'should be added according to what you would like displayed)
SET DbObj = Server.CreateObject("ADODB.Connection") 'set up the
'ADO connection
DbObj.Open "DSN=DSNName","username","password" 'open the connection to
'the database, using DSN - set up the DSN in your control panel
SET oRs = DbObj.Execute(strSQL) 'Execute the SQL statement
%>
<center>
<%
DO WHILE NOT oRs.EOF
on error resume next
%>
<table border="1" width="100%" bordercolor="#00FFFF">
<tr>
<td width="35%">
<table border="0" width="96%">
<tr>
<td width="100%"><font color="#000080" size="4">
Display field description here: <% = oRs.Fields("FieldName") %></td>
</tr>
</table>
</td>
<font color="#000080" size="4">
<td width="65%"> <% = oRs.Fields("FieldName") %></td></font>
</tr>
</table>
<% oRs.MoveNext %>
<% Loop %> 'Go to the next record if not end of file
</center>
<% DbObj.Close 'Close the database connection
SET DbObj = Nothing 'Clean up after yourself
%>
</body>
</html>