日期:2010-04-24  浏览次数:21368 次

  本站原创内容,转载请注明出处网页教学网

  我们可以使用 SQL语句来创建一个查询,实现按条件显示我们需要的数据库中的数据。

  例如显示"Companyname"以 A 开始的公司的名字。下面代码可以实现:

  <html>

  <body>

  <%

  set conn=Server.CreateObject("ADODB.Connection")

  conn.Provider="Microsoft.Jet.OLEDB.4.0"

  conn.Open "e:/webjx/webjx.mdb"

  set rs=Server.CreateObject("ADODB.recordset")

  sql="SELECT Companyname, Contactname FROM Customers WHERE CompanyName LIKE 'A%'"

  rs.Open sql, conn

  %>

  <table border="1" width="100%">

   <tr>

   <%for each x in rs.Fields

    response.write("<th>" & x.name & "</th>")

   next%>

   </tr>

   <%do until rs.EOF%>

    <tr>

    <%for each x in rs.Fields%>

     <td><%Response.Write(x.value)%></td>

    <%next

    rs.MoveNext%>

    </tr>

    <%loop

    rs.close

   conn.close%>

  </table>

  </body>

  </html>

  显示"Companyname"中开始字母大于E的那些公司的信息:

  <html>

  <body>

  <%

  set conn=Server.CreateObject("ADODB.Connection")

  conn.Provider="Microsoft.Jet.OLEDB.4.0"

  conn.Open(Server.Mappath("e:/webjx/webjx.mdb"))

  set rs = Server.CreateObject("ADODB.recordset")

  sql="SELECT Companyname, Contactname FROM Customers WHERE CompanyName>'E'"

  rs.Open sql, conn

  %>

  <table border="1" width="100%">

  <tr>

  <%for each x in rs.Fields

    response.write("<th>" & x.name & "</th>")

  next%>

  </tr>

  <%do until rs.EOF%>

    <tr>

    <%for each x in rs.Fields%>

      <td><%Response.Write(x.value)%> </td>

    <%next

    rs.MoveNext%>

    </tr>

  <%loop

  rs.close

  conn.close

  %>

  </table>

  </body>

  </html>

  显示"Country"为china的公司的信息:

  <html>

  <body>

  <%

  set conn=Server.CreateObject("ADODB.Connection")

  conn.Provider="Microsoft.Jet.OLEDB.4.0"

  conn.Open(Server.Mappath("e:/webjx/webjx.mdb"))

  set rs = Server.CreateObject("ADODB.recordset")

  sql="SELECT Companyname, Contactname FROM Customers WHERE Country='china'"

  rs.Open sql, conn

  %>

  <table border="1" width="100%">

  <tr>

  <%for each x in rs.Fields

    response.write("<th>" & x.name & "</th>")

  next%>

  </tr>

  <%do until rs.EOF%>

    <tr>

    <%for each x in rs.Fields%>

      <td><%Response.Write(x.value)%> </td>

    <%next

    rs.MoveNext%>

    </tr>

  <%loop

  rs.close

  conn.close

  %>

  </table>

  </body>

  </html>

  用户自己选择国家,然后显示相应的信息:

  <html>

  <body>

  <%

  set conn=Server.CreateObject("ADODB.Connection")

  conn.Provider="Microsoft.Jet.OLEDB.4.0"

  conn.Open(Server.Mappath("e:/webjx/webjx.mdb"))

  set rs=Server.CreateObject("ADODB.recordset")

  sql="SELECT DISTINCT Country FROM Customers ORDER BY Country"

  rs.Open sql,conn

  country=request.form("country")

  %>

  <form method="post">

  选择国家:<select name="country">

  <% do until rs.EOF

    response.write("<option")

    if rs.fields("country")=country then

     response.write(" selected")

    end if

    response.write(">")

    response.wri