< %
' ========================================================== ===
' Function: adds, deletes, and modifies any table in the database.
' Author: wangsdong
' Site: http://www.aspprogram.cn
' ArticleFor Original Author, please indicate the source of the article and retain the author's information. Thank you for your support!
' It indicates that tablename is the table name, and STR values are insert, delete, and update.
' The statements to be executed are add, delete, and modify. ID is an automatically numbered field.
' Examples
' ========================================================== ===
Function Add_del_update (tablename, STR, ID)
Select Case Str
Case " Insert " :
SQL = " Select * from [ " & Tablename & " ] Where id = NULL "
Rs. Open SQL, Conn, 1 , 3
Rs. addnew
For Each Key in request. Form
RS ( CSTR (Key )) = Request (key)
Next
Rs. Update
Rs. Close
Case " Update " :
SQL = " Select * from [ " & Tablename & " ] Where id = " & ID
Rs. Open SQL, Conn, 1 , 3
For Each Key in request. Form
If Key <> " ID " Then
RS ( CSTR (Key )) = Request (key)
End If
Next
Rs. Update
Rs. Close
Case " Delete " :
SQL = " Delete from [ " & Tablename & " ] Where ID in ( " & ID & " ) "
Rs. Open SQL, Conn, 1 , 3
Case ""
End Select
End Function
% >
Prerequisites: The table must have an automatically added field with the field name ID. The form name before submission must be the same as the field name in the database, and the name attribute cannot be used as a button.
Use Case:
< %
' Add a record to Table 1
Call Add_del_update ( " Table1 " , " Insert " , "" )
' Modify the id = 5 (the ID is placed in the previous action or a hidden form) record in table 1.
ID = Request ( " ID " )
Call Add_del_update ( " Table1 " , " Update " , ID)
' Delete some records in table 1. The ID is in the previous action or a form.
ID = Request ( " ID " )
Call Add_del_update ( " Table1 " , " Delete " , ID)
% >