asp连接access数据库应用下面代码
<%
set conn=Server.CreateObject("ADODB.Connection")
DBPath = Server.MapPath("board.mdb") 'Server.MapPath("board.mdb") 获得数据库文件board.mdb的绝对路径
conn.Open "provider=microsoft.jet.oledb.4.0;data source="&dbpath
%>
当然这个代码完全没有考虑安全性,安全性我们以后再讨论
首先在board.mdb数据库里建立一张数据表board(id,title,content,subtime)个字段数据类型自己思考,环境都建好了,下面我们就开始程序设计,无论网页还是程序我建议用dw来做吧,我就是用它的
本例中涉及到的文件有
conn.asp数据库链接文件
send.asp,发表留言界面页
sendok.asp,留言录库操作程序文件
board.asp留言读库显示页面
文件的内容附件里有源文件大家可以下载察看
首先介绍asp一个很有效的特性就是服务器端包含
<!--#i nclude file="conn.asp"-->
其中conn.asp就是被包含的文件,此包含可以出现在文件的任意位置
被包含的文件内容将完全被解释成包含文件的内容,应用此特性我们可以将各个文件中相同的部分提取出来保存成一个文件,再由各个文件包含此文件达到内容共享的目的,由此也可将整个项目中,常用的函数、全局变量、类等等保存一个文件,然后由其他文件包含到自己的内容里,这样一来,整个项目将会很有条理,重复的代码也会大大降低。
conn.asp内容
<%
set conn=Server.CreateObject("ADODB.Connection")
DBPath = Server.MapPath("board.mdb")
conn.Open "provider=microsoft.jet.oledb.4.0;data source="&dbpath
%>
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
send.asp内容
<html>
<head>
<meta http-equiv="Content-Type" c>
<title>无标题文档</title>
<style type="text/css">
<!--
.style1 {font-size: 18px}
-->
</style>
</head>
<body>
<table width="700" border="0" align="center">
<form name="form1" method="post" action="sendok.asp">
<tr>
<td><div align="center" class="style1">发布留言</div></td>
</tr>
<tr>
<td align="center">标题:
<input name="title" type="text" size="50"></td>
</tr>
<tr>
<td align="center">内容:
<textarea name="content" cols="50"></textarea></td>
</tr>
<tr>
<td align="center"><input type="submit" name="Submit" value="提交"></td>
</tr>
</form>
</table>
</body>
</html>
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
sendok.asp内容
<!--#i nclude file="conn.asp"-->
<%
title=request.form("title")
content=request.form("content")
subtime=now()
conn.execute("insert into board (title,content,subtime) values('"&title&"','"&content&"','"&subtime&"')")
%>
<script>
alert("留言成功!");
location.href="/board.asp";
</script>
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
board.asp内容
<!--#i nclude file="conn.asp"-->
<style type="text/css">
<!--
.style2 {
font-size: 16px;
font-weight: bold;
}
-->
</style>
<table width="300" border="0" align="center">
<tr>
<td align="center"><span class="style2">留言板查看</span></td>
</tr>
</table>
<br>
<br>
<table width="200" border="0" align="center">
<tr>
<td align="center"><a href="/send.asp">发表留言</a></td>
</tr>
</table>
<br>
<br>
<%
set rs=conn.execute("select * from board order by id desc")
do while not rs.eof
%>
<table width="600" border="0" align="center" cellspacing="1" bgcolor="#999999">
<tr bgcolor="#FFFFFF">
<td width="447"><%=rs("title")%></td>
<td width="146"><%=rs("subtime")%></td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2"><%=rs("content")%></td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2"> </td>
</tr>
</table>
<%
rs.movenext
loop
rs.close
set rs=nothing
conn.close
set conn=nothing
%>
“ASP从入门到精通”精心收集了一些ASP实例教程,有“在学习asp留言板的设计制作中学习asp”、“手把手教你利用ASP打造自己的网站论坛 ”、“在线考试系统实例教程”等,旨在为ASP爱好者提供一个交流学习的环境,欢迎所有喜欢ASP的朋友加入我们的论坛。本站诚征各版版主。如果您有能力有兴趣,请到站务管理留贴,注明您想承担版主的版块。我会在第一时间给您设置版主。有什么好的建议或意见,也请不吝指教。