我在HTML表单中显示查询结果时遇到问题。当我处理此代码时,它在我的HTML表格中显示空白单元格。任何提示或建议将不胜感激。
ASP:
<%
dim query, myDSN, Connect, rs
myDSN = "PROVIDER=MICROSOFT.ACE.OLEDB.12.0;DATA SOURCE=" & Server.Mappath("/MasterDB.accdb")
query = "SELECT * FROM DAY_DATA;"
Set Connect = Server.CreateObject("ADODB.Connection")
Connect.Open myDSN
Set rs = Server.CreateObject("ADODB.recordset")
rs.Open query, Connect
%>
结果页面HTML:
<table border="1" width="100%">
<tr>
<%for each x in rs.Fields
response.write("<th>" & x.name & "</th>")
next%>
</tr>
<%do until rs.EOF%>
<tr>
<%for each x in rs.Fields%>
<td><%Response.Write(x.value)%></td>
<%next
rs.MoveNext%>
</tr>
<%loop
rs.close
conn.close
%>
</table>
如果我可以使用它,我将用一个用它们的输入生成的查询字符串替换它。谢谢你的阅读!
答案 0 :(得分:1)
我创建了这个函数来完成这个任务...希望这会有所帮助:
Sub WriteRS(ByRef myRS)
Dim i, fCount
Response.Write ("<br><br clear=""all""><table cellspacing=1 cellpadding=5 border=1><tr>")
For i = 0 To myRS.Fields.Count - 1
Response.Write ("<td nowrap>" & myRS.Fields(i).name & "</td>" & vbNewLine)
Next
Response.Write ("</tr>" & vbNewLine)
If Not myRS.BOF THEN myRS.MoveFirst
fCount = myRS.Fields.Count
Do Until myRS.EOF
Response.Write ("<tr>" & vbNewLine)
For i = 0 To fCount - 1
Response.Write ("<td nowrap>" & CheckValue(Trim(myRS.Fields.Item(i).Value), " ") & "</td>" & vbNewLine)
Next
Response.Write ("</tr>" & vbNewLine)
Response.Flush
myRS.MoveNext
Loop
If Not myRS.BOF THEN
myRS.MoveFirst
Else
Response.Write ("<tr><td colspan=""" & fCount & """>No Data Found</td</tr>" & vbNewLine)
End If
Response.Write ("</table><br clear=""all"">")
End Sub
希望这有帮助。
答案 1 :(得分:0)
尝试将x.value
替换为rs(x.name)
。
<%do until rs.EOF%>
<tr>
<%for each x in rs.Fields%>
<td><%=rs(x.Name)%></td>
<%next
rs.MoveNext%>
</tr>
<%loop%>
如果x.Name
没有返回任何内容(也就是说您没有看到表中的列名),请尝试摆弄光标类型和锁定类型。
rs.Open query, Connect, 1, 2