ADO 查询

我们可以使用 SQL 来创建查询,这样就可以指定仅查看选定的记录和字段。

显示 "Companyname" 以 A 开头的记录

如何仅仅显示 "Customers" 表的 "Companyname" 字段中以 A 开头的记录。

  1. <html>
  2. <body>
  3. <%
  4. set conn=Server.CreateObject("ADODB.Connection")
  5. conn.Provider="Microsoft.Jet.OLEDB.4.0"
  6. conn.Open(Server.Mappath("/db/northwind.mdb"))
  7. set rs = Server.CreateObject("ADODB.recordset")
  8. sql="SELECT Companyname, Contactname FROM Customers WHERE CompanyName LIKE 'A%'"
  9. rs.Open sql, conn
  10. %>
  11. <table border="1" width="100%">
  12. <tr>
  13. <%for each x in rs.Fields
  14. response.write("<th>" & x.name & "</th>")
  15. next%>
  16. </tr>
  17. <%do until rs.EOF%>
  18. <tr>
  19. <%for each x in rs.Fields%>
  20. <td><%Response.Write(x.value)%></td>
  21. <%next
  22. rs.MoveNext%>
  23. </tr>
  24. <%loop
  25. rs.close
  26. conn.close
  27. %>
  28. </table>
  29. </body>
  30. </html>

显示 "Companyname" 大于 E 的记录

如何仅仅显示 "Customers" 表的 "Companyname" 字段中大于 E 的记录。

  1. <html>
  2. <body>
  3. <%
  4. set conn=Server.CreateObject("ADODB.Connection")
  5. conn.Provider="Microsoft.Jet.OLEDB.4.0"
  6. conn.Open(Server.Mappath("/db/northwind.mdb"))
  7. set rs = Server.CreateObject("ADODB.recordset")
  8. sql="SELECT Companyname, Contactname FROM Customers WHERE CompanyName>'E'"
  9. rs.Open sql, conn
  10. %>
  11. <table border="1" width="100%">
  12. <tr>
  13. <%for each x in rs.Fields
  14. response.write("<th>" & x.name & "</th>")
  15. next%>
  16. </tr>
  17. <%do until rs.EOF%>
  18. <tr>
  19. <%for each x in rs.Fields%>
  20. <td><%Response.Write(x.value)%> </td>
  21. <%next
  22. rs.MoveNext%>
  23. </tr>
  24. <%loop
  25. rs.close
  26. conn.close
  27. %>
  28. </table>
  29. </body>
  30. </html>

仅显示西班牙的客户

如何仅仅显示 "Customers" 表中的西班牙客户。

  1. <html>
  2. <body>
  3. <%
  4. set conn=Server.CreateObject("ADODB.Connection")
  5. conn.Provider="Microsoft.Jet.OLEDB.4.0"
  6. conn.Open(Server.Mappath("/db/northwind.mdb"))
  7. set rs = Server.CreateObject("ADODB.recordset")
  8. sql="SELECT Companyname, Contactname FROM Customers WHERE Country='China'"
  9. rs.Open sql, conn
  10. %>
  11. <table border="1" width="100%">
  12. <tr>
  13. <%for each x in rs.Fields
  14. response.write("<th>" & x.name & "</th>")
  15. next%>
  16. </tr>
  17. <%do until rs.EOF%>
  18. <tr>
  19. <%for each x in rs.Fields%>
  20. <td><%Response.Write(x.value)%> </td>
  21. <%next
  22. rs.MoveNext%>
  23. </tr>
  24. <%loop
  25. rs.close
  26. conn.close
  27. %>
  28. </table>
  29. </body>
  30. </html>

让用户来选择筛选标准

让用户根据国别来选择客户

  1. <html>
  2. <body>
  3. <%
  4. set conn=Server.CreateObject("ADODB.Connection")
  5. conn.Provider="Microsoft.Jet.OLEDB.4.0"
  6. conn.Open(Server.Mappath("/db/northwind.mdb"))
  7. set rs=Server.CreateObject("ADODB.recordset")
  8. sql="SELECT DISTINCT Country FROM Customers ORDER BY Country"
  9. rs.Open sql,conn
  10. country=request.form("country")
  11. %>
  12. <form method="post">
  13. Choose Country <select name="country">
  14. <% do until rs.EOF
  15. response.write("<option")
  16. if rs.fields("country")=country then
  17. response.write(" selected")
  18. end if
  19. response.write(">")
  20. response.write(rs.fields("Country"))
  21. rs.MoveNext
  22. loop
  23. rs.Close
  24. set rs=Nothing %>
  25. </select>
  26. <input type="submit" value="Show customers">
  27. </form>
  28. <%
  29. if country<>"" then
  30. sql="SELECT Companyname,Contactname,Country FROM Customers WHERE country='" & country & "'"
  31. set rs=Server.CreateObject("ADODB.Recordset")
  32. rs.Open sql,conn
  33. %>
  34. <table width="100%" cellspacing="0" cellpadding="2" border="1">
  35. <tr>
  36. <th>Companyname</th>
  37. <th>Contactname</th>
  38. <th>Country</th>
  39. </tr>
  40. <%
  41. do until rs.EOF
  42. response.write("<tr>")
  43. response.write("<td>" & rs.fields("companyname") & "</td>")
  44. response.write("<td>" & rs.fields("contactname") & "</td>")
  45. response.write("<td>" & rs.fields("country") & "</td>")
  46. response.write("</tr>")
  47. rs.MoveNext
  48. loop
  49. rs.close
  50. conn.Close
  51. set rs=Nothing
  52. set conn=Nothing%>
  53. </table>
  54. <% end if %>
  55. </body>
  56. </html>

显示选定的数据

我们希望仅仅显示 "Customers" 表的 "Companyname" 字段中以 A 开头的记录:

  1. <html>
  2. <body>
  3.  
  4. <%
  5. set conn=Server.CreateObject("ADODB.Connection")
  6. conn.Provider="Microsoft.Jet.OLEDB.4.0"
  7. conn.Open "c:/webdata/northwind.mdb"
  8.  
  9. set rs=Server.CreateObject("ADODB.recordset")
  10. sql="SELECT Companyname, Contactname FROM Customers
  11. WHERE CompanyName LIKE 'A%'"
  12. rs.Open sql, conn
  13. %>
  14.  
  15. <table border="1" width="100%">
  16. <tr>
  17. <%for each x in rs.Fields
  18. response.write("<th>" & x.name & "</th>")
  19. next%>
  20. </tr>
  21. <%do until rs.EOF%>
  22. <tr>
  23. <%for each x in rs.Fields%>
  24. <td><%Response.Write(x.value)%></td>
  25. <%next
  26. rs.MoveNext%>
  27. </tr>
  28. <%loop
  29. rs.close
  30. conn.close%>
  31. </table>
  32.  
  33. </body>
  34. </html>