ADO 排序

我们可以使用SQL来规定如何对记录集中的数据进行排序。

实例

根据指定的字段名处对记录进行升序排序

如何根据指定字段名对数据进行排序

  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 ORDER BY CompanyName"
  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 Companyname, Contactname FROM Customers ORDER BY CompanyName DESC"
  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. <table border="1" width="100%" bgcolor="#fff5ee">
  4. <tr>
  5. <th align="left" bgcolor="#b0c4de">
  6. <a href="/example/adoe/demo_adoe_sort_3.asp?sort=companyname">Company</a>
  7. </th>
  8. <th align="left" bgcolor="#b0c4de">
  9. <a href="/example/adoe/demo_adoe_sort_3.asp?sort=contactname">Contact</a>
  10. </th>
  11. </tr>
  12. <%
  13. if request.querystring("sort")<>"" then
  14. sort=request.querystring("sort")
  15. else
  16. sort="companyname"
  17. end if
  18. set conn=Server.CreateObject("ADODB.Connection")
  19. conn.Provider="Microsoft.Jet.OLEDB.4.0"
  20. conn.Open(Server.Mappath("/db/northwind.mdb"))
  21. set rs=Server.CreateObject("ADODB.recordset")
  22. sql="SELECT Companyname,Contactname FROM Customers ORDER BY " & sort
  23. rs.Open sql,conn
  24. do until rs.EOF
  25. response.write("<tr>")
  26. for each x in rs.Fields
  27. response.write("<td>" & x.value & "</td>")
  28. next
  29. rs.MoveNext
  30. response.write("</tr>")
  31. loop
  32. rs.close
  33. conn.close
  34. %>
  35. </table>
  36. </body>
  37. </html>

对数据进行排序

我们希望显示 "Customers" 表中的"Companyname"和"Contactname"字段,并根据"Companyname"进行排序(请记得用.asp为后缀保存):

  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
  11. Customers ORDER BY CompanyName"
  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>