菜鸟学ADO实例教程之ADO 更新记录

2008-02-23 09:31:05来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

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

  我们可以使用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>

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:菜鸟学ADO实例教程之ADO 排序数据

下一篇:菜鸟学ADO实例教程之ADO 删除记录