欢迎来到HELLO素材网! 南京网站制作选择顺炫科技
丰富的DIV CSS模版、JS,jQuery特效免费提供下载
当前位置:主页 > 建站教程 > 网站制作教程 >

ADO初学者教程:ADO 更新记载

发表于2019-05-15 15:36| 次阅读| 来源网络整理| 作者session

摘要:ADO初学者教程:ADO 更新记载
ADO初学者教程:ADO 更新记载

咱们可利用SQL的UPDATE来更新数据库表中的某条记载。

更新数据库表中的记载

咱们宿愿更新Northwind数据中Customers表的某条记载。首先咱们需求创建一个表格,来列出Customers中的一切记载。

<html><body><%set conn=Server.CreateObject("ADODB.Connection")conn.Provider="Microsoft.Jet.OLEDB.4.0"conn.Open "c:/webdata/northwind.mdb"set rs=Server.CreateObject("ADODB.Recordset")rs.open "SELECT * FROM customers",conn%><h2>List Database</h2><table><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" value="<%=x.value%>"> </td> <%else%> <td><%Response.Write(x.value)%></td> <%end ifnext%></form><%rs.MoveNext%></tr><%loopconn.close%></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 "c:/webdata/northwind.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 value="<%=x.value%>"></td> <%next%> </tr> </table> <br /><br /> <input type="submit" value="Update record"> </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 ifconn.close%></body></html>