连接SQL Server时出现空白页面 - 经典ASP

3

我需要帮助,当使用classic asp连接数据库时,一直出现空白页面。 为了安全起见,我更改了一些连接字符串。 我知道这个字符串有效,因为我可以在vb.net中连接它。

    <%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Search Test</title>
</head>

<body>
<% 
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open "Data Source=ServerIP;Initial Catalog=Database;User ID=Username;Password=Password"

strSql = "SELECT * FROM categories Where idParentcategory=1 ORDER BY categorydesc ASC"

Set rs = Conn.Execute(strSql)

If rs.eof Then
   Response.write("No records returned")
End If

do until rs.eof
   Response.write(rs("categorydesc") &  "<br>")
   rs.movenext
loop

Conn.Close
Set Conn = Nothing
 %>
</body>
</html>
2个回答

1
如果页面显示为空白,有可能您使用了on error resume next,但我们无法看到它(也许它在一个包含文件中)?
我会通过检查eof,然后仅在结果集不为空时才循环遍历来处理此问题。
Set rs = Conn.Execute(strSql)

If rs.eof Then
  Response.write("No records returned")
Else
  do until rs.eof
    Response.write(rs("categorydesc") &  "<br>")
    rs.movenext
  loop
End If

rs.Close : set rs = nothing
Conn.Close : set Conn = Nothing

这与 @03Usr 的回答并没有太大区别,除了:

  1. 您应该能够确定页面为什么显示为空白。
  2. 除了关闭和处理连接对象之外,您还应该关闭和处理记录集对象(即使它是空的)。

0

试试这个:

If not rs.eof then
Do while not rs.eof or rs.bof 
      Response.write(rs("categorydesc") &  "<br>")
    rs.movenext
    Loop
 Else
      Response.Write "No records found"
 End If

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接