0

我有这个代码的问题我没有错误,但结果是一个空白的白色输出,请帮助为什么会发生这种情况,几天前代码工作正常,但有一天它会导致空白输出。我将此代码托管在支持较旧的 asp 2.0 和 ms access 2003 具有 .mdb 扩展文件的服务器上

 <html>
    <body>
    <div id ="pin">
    <%

      city= Request.Form("username")
      area = Request.Form("password")

        Dim Conn,rs,strSQL

        Set Conn = Server.CreateObject("ADODB.Connection")
         Conn.Open"Provider= Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("/pinner/db/database1.mdb") 
        'Conn.Open"DRIVER=Microsoft Access Driver(*.mdb);DBQ=" & Server.MapPath("/pinner/db/Database1.mdb")
        'strSQL = "SELECT pincodes.officename FROM pincodes where pincode = '" & city & "';"
        Set rs = Server.CreateObject("ADODB.Recordset")

        strSQL = "Select  pincode from pincodes  WHERE Districtname = '" city "' AND officename = '" area "';"
        'strSQL = "Select  pincode from pincodes  WHERE (Districtname  like "&city& *" AND officename like "&area&*");"
        rs.Open strSQL,Conn

        If (rs.EOF And rs.BOF) Then
        Response.Write ("<br>")
        Response.Write ("[pincode] not found")
        Response.Write ("<br>")
        Else
        Response.Write ("<br>")
        Response.Write (rs("pincode"))
        Response.Write ("<br>")
        End If




    rs.close

     Set rs = Nothing
     Set Conn = Nothing

    %>
    </div>
    </body>
    </html>
4

2 回答 2

2

由于您无法保证 SELECT 查询将返回任何行,因此您必须测试EOF记录集的属性。换句话说,您需要执行以下操作:

' ...
rs.Open strSQL,Conn
If rs.EOF Then
    Response.Write ("<br>")
    Response.Write ("[pincode] not found")
    Response.Write ("<br>")
Else
    Response.Write ("<br>")
    Response.Write (rs("pincode"))
    Response.Write ("<br>")
End If
' ...
于 2013-09-12T20:30:44.160 回答
0

这是一个更完整的示例,说明如何遍历您的记录。希望这能让你上路。

Sub TestIt
  Dim Conn
  Dim RST
  Dim dbError
  Dim dbCredDB
  Dim dbCredServer
  Dim dbCredUser
  Dim dbCredPass
  Dim queryString

  ' Set up Parameters
  dbCredDB = "TestDB"
  dbCredServer = "testserver"
  dbCredUser = "TestUser"
  dbCredPass = "TestPass"
  queryString = "SELECT 'Test' AS Test FROM DUAL"

  ' Open Connection
  Set Conn = CreateObject("ADODB.Connection")
' ** Oracle Connection Properties
  Conn.Provider = "OraOLEDB.Oracle"
  Conn.Properties("Data Source") = dbCredDB
  Conn.Properties("User ID") = dbCredUser
  Conn.Properties("Password") = dbCredPass
' ** SQL Server Connection Properties
  ' Conn.Provider = "SQLNCLI11"
  ' Conn.Properties("Server") = dbCredServer
  ' Conn.Properties("Database") = dbCredDB
  ' Conn.Properties("Uid") = dbCredUser
  ' Conn.Properties("Pwd") = dbCredPass
  Conn.Open

  ' Open Recordset
  Set RST = CreateObject("ADODB.Recordset")
  Set RST.ActiveConnection = Conn
  RST.CursorType = 0 'adOpenForwardOnly
  RST.Open queryString, Conn

  ' Trap for Server Errors
  Err.Clear
  On Error Resume Next

  ' Run through each Record in Recordset - write value
  Do Until RST.EOF
    Response.Write (RST.Fields("Test").value + "<br>")

    ' Check for error
    if err.Number <> 0 Then
      For Each dbError in Conn.Errors
        Response.Write ("Error! " + dbError.Description + "<br>")
      Next
      Exit Do
    End If

    ' Move to next record
    RST.MoveNext
  Loop

  ' Stop Trapping for Errors
  On Error Goto 0

  ' Close Recordset and Connection
  RST.Close
  Conn.Close

  ' Free up references
  set RST = Nothing
  set Conn = Nothing

End Sub

微软还发表了许多关于如何实现这一目标的文章。

如何从 ASP 中翻页记录集 http://support.microsoft.com/kb/202125

于 2013-09-12T20:48:57.883 回答