菜鳥學ADO執行個體教程之ADO 更新記錄

來源:互聯網
上載者:User
ado|教程

  本站原創內容,轉載請註明出處網頁教學網。

  我們可以使用SQL UPDATE 命令來更新資料庫中的記錄。

  我們想更新資料庫webjx中的一個表Customers中的一個記錄,首先我們把表Customers中的所有記錄顯 示出來。

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

  rs.open "SELECT * FROM customers",conn

  %>

  <h2>List Database</h2>

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

  <tr>

  <%

  for each x in rs.Fields

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

  next

  %>

  </tr>

  <% do until rs.EOF %>

  <tr>

  <form method="post" action="demo_update.asp">

  <%

  for each x in rs.Fields

   if lcase(x.name)="customerid" then%>

    <td>

    <input type="submit" name="customerID" value="<%=x.value%>">

    </td>

   <%else%>

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

   <%end if

  next

  %>

  </form>

  <%rs.MoveNext%>

  </tr>

  <%

  loop

  conn.close

  %>

<br/>

  </table>

  </body>

  </html>

  當使用者點擊了 "customerID" 按鈕就要調用"demo_update.asp":

  <html>

  <body>

  <h2>Update Record</h2>

  <%

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

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

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

  cid=Request.Form("customerID")

  if Request.form("companyname")="" then

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

   rs.open "SELECT * FROM customers WHERE customerID='" & cid & "'",conn

   %>

   <form method="post" action="demo_update.asp">

   <table>

   <%for each x in rs.Fields%>

   <tr>

   <td><%=x.name%></td>

   <td><input name="<%=x.name%>" value="<%=x.value% >"></td>

   <%next%>

   </tr>

   </table>

   <br /><br />

   <input type="submit" value="更新記錄">

   </form>

  <%

  else

   sql="UPDATE customers SET "

   sql=sql & "companyname='" & Request.Form("companyname") & "',"

   sql=sql & "contactname='" & Request.Form("contactname") & "',"

   sql=sql & "address='" & Request.Form("address") & "',"

   sql=sql & "city='" & Request.Form("city") & "',"

   sql=sql & "postalcode='" & Request.Form("postalcode") & "',"

   sql=sql & "country='" & Request.Form("country") & "'"

   sql=sql & " WHERE customerID='" & cid & "'"

   on error resume next

   conn.Execute sql

   if err<>0 then

    response.write("No update permissions!")

   else

    response.write("Record " & cid & " was updated!")

   end if

  end if

  conn.close

  %>

<br/>

  </body>

  </html>



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.