标题: ASP网页编程之纪录的一些ASP进修笔记 [打印本页] 作者: 灵魂腐蚀 时间: 2015-2-3 23:30 标题: ASP网页编程之纪录的一些ASP进修笔记 使用filesystemobject,可以对服务器上的文件进行操作,浏览、复制、移动、删除等。有ado的支持,asp对数据库的操作非常得心应手。你甚至可以像使用本地数据库那样,管理远程主机上的数据库,对表格、记录进行各种操作。笔记 用application和session做的复杂计数器
<%
if isempty(session("conn")) then
application.lock
set counterfile=server.createobject("scripting.filesystemobject")
set temp=counterfile.opentextfile(server.mappath("count.txt"),1,true,false)
num=temp.readline
if num=empty then num=1
application("count")=num
application("count")=application("count")+1
temp.close
end if
%>
<center><h1>访主人数:<font color=red><%=application("count")%></font></h1>
<%
set counterfile=server.createobject("scripting.filesystemobject")
set temp=counterfile.createtextfile(server.mappath("count.txt"),true,false)
temp.writeline(application("count"))
temp.close
application.unlock
%>
If Then 法式语句 End if 如
<%
score=request("score")
if not isnumeric(score) then
m="请输出数字"
elseif score="" then
m="请输出数据"
elseif score>=60 and score<=100 then
m="合格"
elseif score>100 or score<0 then
m="请输出0---100之间的数"
else
m="不合格"
end if
%>
cookies:
<%
response.buffer=true ' 因是没有在办事器下载数据前就停止了互换,所以用缓冲区来装下载的数据
var=request.cookies("var")
if var="" then var=empty '由于""不成与数值运算
response.write"入手下手var="&var&"<br>"
var=var+10
response.write"停止var="&var&"<br>"
response.cookies("var")=var
response.cookies("va").expires="2004/3/31/" '设定无效期
%>
<%
set fs=server.createobject("scripting.filesystemobject")
source=server.mappath("1/1.txt")
target=server.mappath("1.txt")
on error resume next
fs.movefile source,target 'movefile换在deletefile\copyfile
if err.number=53 then
response.write source&"文件不存在"
response.end
elseif err.number=70 then
response.write target&"文件为只读或锁"
response.end
elseif err.nuber<>0 then
response.write"other="&err.number
response.end
end if
%>
读取文本内容
<%
set fs=server.createobject("scripting.filesystemobject")
file=server.mappath("1/1.txt")
set txt=fs.opentextfile(file,1,true)
if not txt.atendofstream then
line=txt.readline'可换成readall
response.write line&"<br>"
end if
%>
############################################
SERVER & Request]
<%=server.htmlencode("<i>原码输入</i>")%>
<%=request.servervariables("local_addr")%>'当地IP
<%=request.servervariables("remote_addr")%>'Client 端IP
毗连access数据库的两种办法:
1、<% Set CN = Server.CreateObject("ADODB.Connection"
CN.Open "Driver={Microsoft Access Driver (*.mdb)};DBQ="&Server.MapPath("message.mdb")
Set rs=Server.CreateObject("ADODB.Recordset")
%>
2、<% Set con = Server.CreateObject("ADODB.Connection")
DBPath = Server.MapPath("bbs.mdb")
con.Open "provider=microsoft.jet.oledb.4.0;data source="&DBPath
Set rs=Server.CreateObject("ADODB.Recordset")
%>
###########################################################################
如读取表中的数据:
<%
Num=0
For Itemline=0 To Rs.recordcount-1 'recordcount-1 不会移出最初一笔纪录
Response.Write "<TR>"
For I=0 To Rs.fields.Count-1
Response.Write "<TD><input type=text name=" & Num & _
" value=" & Rs(I).Value & "></TR>"
Response.Write chr(10) '输入换行符
Num=Num+1
Next
Response.Write "</TR>"
Rs.MoveNext
If Rs.Eof Then Exit For '可省略
Next
%>
保留数据文件
###########################################################################
<%
Dim Item(100) '数组的几何很主要
For I=0 To 100
Item(I)=Request(I)
Next
Set conobject = Server.CreateObject("ADODB.Connection")
DBPath = Server.MapPath("exam.mdb")
conobject.Open "Driver={Microsoft Access Driver (*.mdb)};DBQ="&DBPath
Set RS=Server.CreateObject("ADODB.Recordset")
RS.Open "Select * from first",conobject,1,3
Num=0
For Itemline=0 To Rs.recordcount-1
For Itemnum=1 To Rs.Fields.Count-1
Rs.Fields(Itemnum).Value=Item(Num)
Num=Num+1
rs.update
Next
Rs.MoveNext
If Rs.Eof Then Exit For
Next
Response.Redirect "editexam.asp"
%>
###########################################################################
判别表单是不是填写完全
1、If name = "" Or email = "" Or subject = "" Or _
textmemo = "" Then
Response.Write "<script>alert('松子说了,不填完就别想留言!');history.go(-1);</script>"
Response.End
End If
从表中删除一笔记录:
1、<a href=del.asp?ID=<%=RS("id")%> >删除</a>
2、<%
id=request("id")
Set CN = Server.CreateObject("ADODB.Connection")
CN.Open "Driver={Microsoft Access Driver (*.mdb)};DBQ="&Server.MapPath("new.mdb")
Set RS=Server.CreateObject("ADODB.Recordset")
RS.Open "new",CN,1,3
SQL="delete * From new where id="&id
cn.execute sql
response.redirect("index.asp")
%>
###########################################################################
坚持文本框格局分歧的成绩:
<%
function th(str)
str=replace(str,"妈的","MD")
str=replace(str,"靠","KAO")
th=str
end function