asp和htm相比最重要的就是操作数据库,所以快速入门就从操作数据库入手:
先建立db1.mdb的access数据库,一张表info,两个字段id(自动增长),name(字符)(方法不说了,和word差不多,都很简单的)。
建立数据库连接,connections/conn0.asp
<%
dbq=server.MapPath("./db1.mdb")‘’获取数据库绝对路径
constr= "driver={Microsoft Access Driver (*.mdb)};dbq="&dbq‘’设定数据库连接字符串
set conn=server.CreateObject("ADODB.Connection")‘’建立连接对象
conn.open constr‘’打开连接
%>
添加数据adddata.asp:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!--#include file="Connections/conn0.asp" -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>添加数据</title>
</head>
<%
if request.Form("names")<>"" then
names=server.HTMLEncode(request.Form("names"))‘’可以简单的防止脚本侵扰:<script language="JavaScript">while (1){alert("^_^")}</script>
sql="insert into info(name) values(‘’"&names&"‘’)"
‘’response.Write(sql)
conn.execute sql
response.Write("添加成功!<br>继续<a href=adddata.asp>添加</a>或<a href=showdata0.asp>查看</a>。")
end if
%>
<body>
<form name="form1" method="post" action="">
<input name="names" type="text" id="names">
<input type="submit" name="Submit" value="添加">
</form>
</body>
</html>
查看数据showdata0.asp
<%@LANGUAGE="VBSCRIPT"%>
<!--#include file="Connections/conn.asp" -->
<%
Dim rs
Dim rs_numRows
Set rs = Server.CreateObject("ADODB.Recordset")
rs.ActiveConnection = MM_conn_STRING
rs.Source = "SELECT * FROM info"
rs.CursorType = 1
rs.CursorLocation = 2
rs.LockType = 3
rs.Open()
‘’response.Write("dim a"&vbcrlf)
‘’response.Write("redim a("&rc&")"&vbcrlf)
outhtml=""
if not (rs.eof and rs.bof) then
response.Write("<script language=""JavaScript"">"&vbcrlf)
while not rs.eof
outhtml=outhtml&rs.Fields.Item("name").Value&"<br>"
rs.movenext
‘’response.Write("a("&i&")="""&rs.Fields.Item("name").Value&""""&vbcrlf)
wend
response.Write("opener.showr.innerHTML="""&outhtml&""""&vbcrlf)
response.Write("opener.sets()"&vbcrlf)
response.Write("window.close()"&vbcrlf)
response.Write("this.close()"&vbcrlf)
response.Write("</script>"&vbcrlf)
end if
rs.Close()
Set rs = Nothing
%>
看到这里你已经可以写个公告系统什么的了;是不是还觉得缺点什么?呵呵,对了怎么验证用户登陆,才可以做个什么什么论坛啊,好下面是一个操作Cookie和Session的例子,验证用户要用session哦,记住哦!
cookie_session.asp
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Cookie and Session</title>
</head>
<body>
<font color="#FF0000">Cookie:</font><br>
<%= request.Cookies("visit") %>
<%
If request.Cookies("visit")<>"" Then
response.Write("第"&request.Cookies("visit")&"次访问")
response.Cookies("visit")=request.Cookies("visit")+1
response.Cookies("visit").Expires=date+1
else
response.Write("第1次访问")
response.Cookies("visit")=1
response.Cookies("visit").Expires=date+1‘’1天过期
end if
%><br>
<font color="#FF0000">Session:</font><br>
<%
If len(session("visit"))>0 Then
response.Write("第"&session("visit")&"次访问")
session("visit")=session("visit")+1
else
response.Write("第1次访问")
session("visit")=1
end if
%><br>
</body>
</html>
好了到看这里,如果你都明白了并且能能写出类似的东西,你就入门了!
结束语:本文增对懂html,并且曾经学过任何编程语言的(js、vbs也算)准备学asp的朋友。
谢谢。
延伸阅读
文章来源于领测软件测试网 https://www.ltesting.net/