ASP 游标参数详解(ASP记录集)第2/2页
更新时间:2008年11月03日 17:43:55 作者:
经常使用游标,但一些具体的说明不是很清楚,书上的东西太乏味
给你个比较有代表性的程序吧,估计能有所帮助
<%@ LANGUAGE = VBScript%>
<%
'------------------------------------------
'昨天晚上编的简单数据库(ACECESS,有id、password、
'register3个字段)操作,仅支持单表单条件操作,
'不过好像没什么用:)
'col:待查询字段名(必添)
'colvalue:待更新、添加字段的值(没有用""代替)
'table:表名(必添)
'keyid:查询条件的字段名(没有用""代替,可选)
'keyvalue:查询条件字段的值(没有用""代替,可选)
'col和colvalue必须成对出现
'keyid和keyvalue必须同时出现
'------------------------------------------
%>
<%
Function DB_execute(col,colvalue,table,keyid,keyvalue,bz)
Dim ConnStr,conn,rs,sql
ConnStr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("data/game.mdb")
Set conn = Server.CreateObject("ADODB.Connection")
conn.open ConnStr
set rs=server.createobject("adodb.recordset")
for i=0 to UBound(col) step 1
if UBound(col)<>i then
sql=sql&col(i)&", "
else
sql=sql&col(i)&" "
end if
next
if keyid <> "" then
sql="select "&sql&"from "&table&" where "&keyid&"='"&keyvalue&"'"
else
sql="select "&sql&"from "&table
end if
select case bz
case "o"
rs.open sql,conn,1,1
dim rsnum
rsnum=rs.RecordCount
if rsnum > 0 then
dim rsArray()
redim rsArray(rsnum,UBound(col))
for i=0 to UBound(col) step 1
rsArray(0,i)=col(i)
next
for i=1 to rsnum step 1
for j=0 to UBound(col) step 1
rsArray(i,j)=rs(col(j))
next
next
DB_execute=rsArray
else
DB_execute="没有查到任何数据"
end if
rs.close
set rs=nothing
case "u"
rs.open sql,conn,3,3
if rs.RecordCount > 0 then
for i=0 to UBound(col) step 1
rs(col(i))=colvalue(i)
next
rs.update
else
DB_execute="没有查到这条数据"
end if
rs.close
set rs=nothing
case "i"
rs.open sql,conn,3,3
rs.addnew
for i=0 to UBound(col) step 1
rs(col(i))=colvalue(i)
next
rs.update
DB_execute="添加数据成功"
rs.close
set rs=nothing
case "d"
sql="delete from "&table&" where "&keyid&"='"&keyvalue&"'"
conn.Execute sql
DB_execute="删除数据成功"
end select
conn.close
set conn=nothing
end Function
%>
<%
'返回记录集
'temp=DB_execute(Array("id","password","register"),"","users","","","o")
'response.write temp(1,2)
'添加一条记录
'temp=DB_execute(Array("id","password","register"),Array("test","test","0"),"users","","","i")
'response.write temp
'更新一条记录
'temp=DB_execute(Array("id","password","register"),Array("new","new","1"),"users","id","test","u")
'response.write temp
'删除一条记录
'temp=DB_execute(Array("id","password","register"),"","users","id","new1","d")
'response.write temp
%>
相关文章
ASP:ActiveX不能创建Scripting.FileSystemObject对象解决办法
关于ActiveX不能创建Scripting.FileSystemObject对象的类似问题,大体上解决办法都是类似的,主要是思想要清晰:首先考虑组件注册问题,其次是组件权限问题,如果服务器配置没有问题的话,那就仔细检查一下你的程序源码吧2011-11-11
最新评论