ADO 实例

显示

显示记录

  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. rs.Open "Select * from Customers", conn
  9. do until rs.EOF
  10. for each x in rs.Fields
  11. Response.Write(x.name)
  12. Response.Write(" = ")
  13. Response.Write(x.value & "<br />")
  14. next
  15. Response.Write("<br />")
  16. rs.MoveNext
  17. loop
  18. rs.close
  19. conn.close
  20. %>
  21. </body>
  22. </html>

在 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. rs.Open "SELECT Companyname, Contactname FROM Customers", conn
  9. %>
  10. <table border="1" width="100%">
  11. <%do until rs.EOF%>
  12. <tr>
  13. <%for each x in rs.Fields%>
  14. <td><%Response.Write(x.value)%></td>
  15. <%next
  16. rs.MoveNext%>
  17. </tr>
  18. <%loop
  19. rs.close
  20. conn.close
  21. %>
  22. </table>
  23. </body>
  24. </html>

向 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"
  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>

向 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"
  9. rs.Open sql, conn
  10. %>
  11. <table border="1" width="100%" bgcolor="#fff5ee">
  12. <tr>
  13. <%for each x in rs.Fields
  14. response.write("<th align='left' bgcolor='#b0c4de'>" & 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" 以 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 的记录

  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>

仅显示中国的客户

  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>

排序

根据指定的字段名称进行升序排序

  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>

Recordset 对象

GetRows

  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. rs.Open "Select * from Customers", conn
  9. 'The first number indicates how many records to copy
  10. 'The second number indicates what recordnumber to start on
  11. p=rs.GetRows(2,0)
  12. response.write("<p>This example returns the value of the first column in the first two records:</p>")
  13. response.write(p(0,0))
  14. response.write("<br />")
  15. response.write(p(0,1))
  16. response.write("<p>This example returns the value of the first three columns in the first record:</p>")
  17. response.write(p(0,0))
  18. response.write("<br />")
  19. response.write(p(1,0))
  20. response.write("<br />")
  21. response.write(p(2,0))
  22. rs.close
  23. conn.close
  24. %>
  25. </body>
  26. </html>

GetString

  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. rs.Open "SELECT Companyname, Contactname FROM Customers", conn
  9. str=rs.GetString(,,"</td><td>","</td></tr><tr><td>"," ")
  10. %>
  11. <table border="1" width="100%">
  12. <tr>
  13. <td><%Response.Write(str)%></td>
  14. </tr>
  15. </table>
  16. <%
  17. rs.close
  18. conn.close
  19. set rs = Nothing
  20. set conn = Nothing
  21. %>
  22. </body>
  23. </html>