application.lock
set counterfile=server.createobject("scripting.filesystemobject")
set t">

记录的一些ASP学习笔记

80酷酷网    80kuku.com

  笔记用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&"
"
var=var+10
response.write"结束var="&var&"
"
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&"
"
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")
%>

##########################################################################

数据表打开的几种方法
1、SQL = "Select * From 数据表 Where 主编号=" & request("ID")
RS.Open SQL,con,1,3
2、RS.Open "数据表",con,1,3
3、RS.Open "Select * from student",con,1,3

读取数据库数据的方面

1、<%=RS(0).name%> '读取第一栏的表头名称
2、<%=RS(1).value%> '读取第二栏的里的内容
3、<%=Rs.Fields(1).Value%> '同上
4、Rs.recordcount '总记录数
5、Rs.fields.Count '有几个栏位

###########################################################################
如读取表中的数据:
<%
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、rs.open ""&s&"",con,1,3
rs.addnew
rs("name")=request("name")
rs("depa")=request("depa")
rs.update
%>

##############################################################

常用函数
一、字符类
1、Asc(string)'该数值为参数字符串第一个字母的ASCII值如Asc(a)=97
2、Chr(charcode)'将指定的字符码转为字符(string) 如chr(97)等于A
3、Len(string)'计算参数中字符串的字符数目如 len(ab c)=4
4、Left(字符串,指定长度) 如left("123 4",3)=123
5、Right 同上
6、MId(字符串,开始字符,指定长度)如mid("123 4",3,1)=1
7、LTrim(字符串)'去除字串中的前置空白如Ltrim(" hello“)="hello"
8、RTrim(字符串)同上
9、Trim(字符串)'去除字符串前、后的空白
10、UCase(字符串)将字符串中所有字符转换为大写字符如Ucase("h")="H"
11、Lcase(字符串)同上
12、Space(空格数目) 如"hello"&space(1)&"jacky"=hello jacky
13、String(字符数,字符)用来产生一个重复字符组成的字符串如string(3,"ab")=aaa'两个以上字符时只重复第一个
14、Join(来源数组[,分隔字符])
15、Split (来源字符串,分隔字符串)
16、replace(源字符串,源字符串中的字符,要变成的字符串)
如 a="long" response.write (replace(a,"l","s"))=song
二、数学类
1、Abs(数值)'返回参数值的绝对值,返回值的类型与参数数值类型相同。
2、Sgn(数值)判断参数数值的正负号,参数为正数时返回1,负数返回-1,0返回0
3、Sqr(数值)求得数值的平方根,不可为负
4、Hex(数值) 转化为16进位
5、Oct (数值) 转化为8进位
三、类别转换
1、Int(数值) 返回数值的整数部分 如int(3,3)=3
2、Fix(数值)同上,只是int(-3.8)=4 而fix(-3.8)=3
3、Str(数值) 将数值转换为字符串。
4、Format(处理对象[,格式])
5、Year(date)/Month(date)/Day(date)取得年、月、日
6、Hour(time)/Minute(time)/Second(time)
7、WeekdayName(1-7)如weekdayname(1)=星期日
8、MonthName(1-12)如monthname(1)=一月
四、比较运算符:= 、<>、<、>、<=、>=
五、Not逻辑运算符:And 、Or、Not
六、指定运算符号:=
七、运算符符号:+、*、/、\(整数除法)2\5=0、Mod(除法取余数)、^乘(次方)5^2=25
八、串拉运算符:& 如abc&efg=abcdefg

从表中删除一条记录:
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

function encode(str)
str = replace(str, ">", ">")
str = replace(str, "<", "<")
str = Replace(str, CHR(32), " ")
str = Replace(str, CHR(13), "")
str = Replace(str, CHR(10) & CHR(10), "</P><P>")
str = Replace(str, CHR(10), "
")
encode=str
end function
%>

<%title=request.form("title")
content=request.form("content")%>

文章标题:<%=th(title)%><hr>
文章内容:<%=encode(th(content))%>


###########################################################################
技巧:
1、response.write chr(10)'输出一个换行符号
2、本页的文件名<%=request.servervariables("path_info")%>
3、with response
<%
with response
.write "this"
.write "is"
.write "a"
.write "test"
end with
%>

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: