ADO 刪除記錄
ADO 刪除記錄是如何實(shí)現(xiàn)的?一起來(lái)看看具體的操作。
我們可使用 SQL 的 DELETE 命令來(lái)刪除數(shù)據(jù)庫(kù)表中的某條記錄。
刪除表中的記錄
我們希望刪除 Northwind 數(shù)據(jù)庫(kù)的 Customers 表中的一條記錄。首先我們需要?jiǎng)?chuàng)建一個(gè)表格,來(lái)列出 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 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_delete.html">
<%
for each x in rs.Fields
if 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
%>
</table>
</body>
</html>
假如用戶點(diǎn)擊 "customerID" 列中的按鈕,會(huì)打開新文件 "demo_delete.asp"。此文件包含了創(chuàng)建輸入域的源代碼,這些輸入域基于數(shù)據(jù)庫(kù)中記錄的字段,同時(shí)也含有一個(gè)刪除當(dāng)前記錄的"刪除按鈕":
<html>
<body>
<h2>Delete 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_delete.html">
<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="Delete record">
</form>
<%
else
sql="DELETE FROM customers"
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 deleted!")
end if
end if
conn.close
%>
</body>
</html>
相關(guān)文章
SQL Delete 語(yǔ)句
更多建議: