ADO Delete Record
ADO Delete Record
We can use the SQL DELETE command to delete a record from a database table.
Deleting records from a table
We want to delete a record from the Customers table in the Northwind database. First, we need to create a table to list all records in Customers.
<%
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
%>
List Database
<%
for each x in rs.Fields
response.write("
next
%>
<% do until rs.EOF %>
<%
for each x in rs.Fields
if x.name="customerID" then%>
<%else%>
<%end if
next
%>
<%rs.MoveNext%>
<%
loop
conn.close
%>
| " & ucase(x.name) & " | ")|
|---|---|
|
|
<%Response.Write(x.value)%> |
If the user clicks the button in the "customerID" column, a new file "demo_delete.asp" will open. This file contains the source code for creating input fields, which are based on the fields recorded in the database, and also includes a "Delete Button" to delete the current record:
Delete Record
<%
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
%>
<%
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
%>