ASP讀取表的欄位名稱及欄位數目

來源:互聯網
上載者:User
 

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
%>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.