实现向数据库中任何表中添加、删除、修改内容

来源:互联网 发布:mac无法登陆app store 编辑:程序博客网 时间:2024/05/16 19:04
<script type="text/javascript"><!--google_ad_client = "pub-4490194096475053";/* 内容页,300x250,第一屏 */google_ad_slot = "3685991503";google_ad_width = 300;google_ad_height = 250;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<%
'===========================================
' 函数功能:实现向数据库中任何表中添加、删除、修改内容
' 参数意义:tablename为表名,str的值是insert,delete,update 表示
' 要执行的语句是添加、删除、修改。id为自动编号类型字段
'===========================================

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
%>


使用前提条件:表中必须有一个字段名为id的自动增加类型的字段提交前的表单名必须和数据库中的字段名一样,且按纽不可以使用name属性。


<%
' 向table1表中添加一条记录
call add_del_update("table1","insert","")
' 修改table1表中的id=5(id放在上一页action中,也可以是隐藏表单)的记录
id=request("id")
call add_del_update("table1","update",id)
' 删除table1表中的一些记录,id是上一页action中,也可以是某一表单
id=request("id")
call add_del_update("table1","delete",id)
%>
原创粉丝点击