·您的位置: 首页 » 资源教程 » 编程开发 » ASP » 菜鸟学ADO实例教程之ADO 更新记录

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

类别: ASP教程  评论数:0 总得分:0
我们可以使用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>
-= 资 源 教 程 =-
文 章 搜 索
关键词:
类型:
范围:
纯粹空间 softpure.com
Copyright © 2006-2008 暖阳制作 版权所有
QQ: 15242663 (拒绝闲聊)  Email: faisun@sina.com
 纯粹空间 - 韩国酷站|酷站欣赏|教程大全|资源下载|免费博客|美女壁纸|设计素材|技术论坛   Valid XHTML 1.0 Transitional
百度搜索 谷歌搜索 Alexa搜索 | 粤ICP备19116064号-1