教程/ASP 十天學會ASP之第六天

來源:互聯網
上載者:User

  學習目的:學會資料庫的基本操作2(查詢記錄)

  在第四天中我們有這樣一個程式:

  <%
  set conn=server.createobject("adodb.connection")
  conn.open "driver={microsoft access driver (*.mdb)};dbq="&server.mappath("example3.mdb")
  exec="select * from guestbook"
  set rs=server.createobject("adodb.recordset")
  rs.open exec,conn,1,1
  %>
  
  我們查詢的是所有的記錄,但是我們要修改、刪除記錄的時候不可能是所有記錄,所有我們要學習檢索合適的記錄。先看一條語句:

  a="張三"
  b=111
  exec="select * from guestbook where name='"+a+"'and tel="+b
  
  where後面加上的是條件,與是and,或是or,我想=,<=,>=,<,>的含義大家都知道吧。這句話的意思就是搜尋name是張三的,並且電話是111的記錄。還有一點就是如果要搜尋一個欄位裡面是不是包含一個字串就可以這麼寫:where instr(name,a)也就是搜尋name裡面有a(張三)這個字串的人。

  我這裡的a,b,是常量,大家可以讓a,b是表單提交過來的變數,這樣就可以做一個搜尋了。

  下面大家看看這個代碼,理解一下:

  <form name="form1" method="post" action="example6.asp">
  搜尋:<br>
  name =
  <input type="text" name="name">
  and tel=
  <input type="text" name="tel">
  <br>
  <input type="submit" name="Submit" value="提交">
  <input type="reset" name="Submit2" value="重設">
  </form>

  example6.asp:
  <%
  name=request.form("name")
  tel=request.form("tel")
  set conn=server.createobject("adodb.connection")
  conn.open "driver={microsoft access driver (*.mdb)};dbq="&server.mappath("example3.mdb")
  exec="select * from guestbook where name='"+name+"' and tel="+tel
  set rs=server.createobject("adodb.recordset")
  rs.open exec,conn,1,1
  %>
  <html>
  <head>
  <title>無標題文檔</title>
  <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
  <table width="100%" border="0" cellspacing="0" cellpadding="0">
  <%
  do while not rs.eof
  %><tr>
  <td><%=rs("name")%></td>
  <td><%=rs("tel")%></td>
  <td><%=rs("message")%></td>
  <td><%=rs("time")%></td>
  </tr>
  <%
  rs.movenext
  loop
  %>
  </table>
  </body>
  </html>

  今天實際上就講了一個where,大家回去做做實驗,把instr()做進去,明天見!



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.