ASP: Ado modify and delete database records
Time added: 2007-4-22
New database records
First, use ADO to connect to the ACCESS database ntopsamp. mdb, use the SQL command to open the "product" Record of the table, and pass the result back to the RS recordset. The ASP code is as follows:
Set conn = server. Createobject ("ADODB. Connection ")
Conn. Open "DBQ =" & server. mappath ("ntopsamp. mdb") & "; driver = {Microsoft Access Driver (*. mdb )};"
Set rs = server. Createobject ("ADODB. recordset ")
Rs. Open "product", Conn, 1, 2
Use the addnew method to add a record. The ASP code is as follows:
Rs. addnew
You can use the following syntax to set the values of fields in a record table:
RS ("field name"). value = Value
RS ("field name") = Value
RS (field order) = Value
For example, the ASP code is as follows:
RS ("name"). value = "laptop"
RS ("quantity") = 100
RS (0) = "12345"
The last update method stores new records in the table.
Rs. Update
Rs. Close
Response. Write ("add record: Name = notebook ")
Modify database records
To modify the database records, use ADO to connect to the ACCESS database ntopsamp. mdb, run the SQL command to find the record to be modified, and pass the result back to the recordset of RS1. The ASP code is as follows:
Set rs = server. Createobject ("ADODB. recordset ")
SQL = "select * from product where name = 'laptop '"
Rs. Open SQL, Conn, 2, 2
If you find the record to be modified and set the values of each field in the table, the last update method will store the newly added record to the table. The ASP code is as follows:
If not Rs. EOF then
Randomize
No1 = round (RND () * 10)
New = "laptop-" & No1
RS ("name"). value = new
Rs. Update
Response. Write ("Modify record to: Name =" & RS1 ("name ")&"")
End if
Delete database records
To delete a database record, use ADO to connect to the ACCESS database ntopsamp. mdb, run the SQL command to find the record to be deleted, and pass the result back to the RS recordset. The ASP code is as follows:
Set rs = server. Createobject ("ADODB. recordset ")
SQL = "select * from product where name = '" & new1 &"'"
Rs. Open SQL, conn1, 1, 3
If you find the record to be deleted and use the next Delete method, you can delete the record in the table. The ASP code is as follows:
If not Rs. EOF then
Rs. Delete
Response. Write ("delete record: Name =" & new1)
End if
Conn. Close