ASP讀取表的欄位名稱及欄位數目
Recordst對象是以二維數組的形式儲存表的記錄,事實上,Recordset對象的每一行都是屬於Fields集合,而Fields集合的每一個項目都是一個Field對象,所以我們能夠利用Fields集合的Count屬性讀取表的欄位數目,然後利用Field對象的Name和Value屬性分別讀取欄位名稱以及欄位資料,由於Value為Fields集合預設的屬性,因此,下面的4種寫法是相同的:1 objRs(i) 2 objRs(i).value 3 objRs.Fields(i) 4 objRs.Fields(i).value
<%OPTION EXPLICIT%>
<!--#include file="common/conn.asp"-->
<%
Dim objRs
Dim fieldCount
Dim i
Dim m
Dim n
Dim temp
Set objRs = objConn.execute("select * from [user]")
'欄位和
fieldCount = objRs.Fields.Count
'輸出欄位和
Response.Write("總共有多少個欄位" & fieldCount & "<hr>")
%>
<table>
<tr>
<!--輸出欄位的名稱-->
<%
For i = 0 to fieldCount - 1
Response.Write("<th>" & objRs.Fields(i).Name & "</th>")
Next
%>
<!--輸出表中欄位的資料-->
<%
Do while not objRs.Eof
temp = "<tr>"
'----迴圈遍曆欄位的值,而不是名字
For n = 0 to fieldCount - 1
temp = temp & "<td>" & objRs.Fields(n).Value & "</td>"
Next
'----End
Response.Write temp & "</tr>"
objRs.movenext
Loop
%>
</tr>
</table>
<%
objRs.close
Set objRs = nothing
objConn.close
Set objConn = nothing
%>