个人做的asp操作access数据库实例,主要模块为产品添加和产品查询。主要有三个页面,包括表单提交与查询页面、产品添加asp脚本页面、产品查询asp脚本。
form.htm的代码如下:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script language="javascript">
<!--
-->
</script>
</head>
<body>
<p>产品添加模块:</p>
<hr />
<form action="add.asp" method="post" enctype="" name="chanpin" target="_self">
<div>
<br />产品代号:<input name="productNum" type="text" size="20" maxlength="30" /><br /><br />
产品名称:<input name="productName" type="text" size="20" maxlength="30" /><br /><br />
产品描述:<!--<input name="productDescribe" type="hidden" value="" /> -->
<br /><br />
<textarea name="productdescribe" cols="50" rows="10"></textarea>
<br /><br />
产品价格:<input name="productPrice" type="text" value="" /><br /><br />
<input name="addProduct" value="产品入库" type="button" οnclick="javascript:submit();return true;" />
<hr />
</div>
</form>
<p>产品查看模块:</p>
<form action="chanp.asp" method="post">
<div>
<hr />
<input name="select" type="submit" value="查询" />
</div>
</form>
</body>
</html>
产品添加脚本add.asp的代码:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<!-- TemplateBeginEditable name="doctitle" -->
<title>无标题文档</title>
<!-- TemplateEndEditable -->
<!-- TemplateBeginEditable name="head" -->
<!-- TemplateEndEditable -->
</head>
<body>
正在提交的产品:<br />
<%
dim productNum,productName,productdescribe,productPrice,recordTime
productNum=request.Form(1)
productName=request.Form(2)
productdescribe=request.Form(3)
productPrice=request.Form(4)
recordTime=now
'for i=1 to request.Form.count
' response.Write(request.form(i)&"<br />")
'next
On Error Resume Next
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
dbpath=Server.MapPath("test.mdb")
conn.Open dbpath
set rs = Server.CreateObject("ADODB.recordset")
sql="insert into chanpin(产品代号,产品名称,产品描述,产品价格,记录日期) values('"&productNum&"','"&productName&"','"&productdescribe&"','"&productPrice&"','"&recordTime&"')"
rs.Open sql, conn
response.Write("<br>产品代号:"&productNum&"<br>产品名称:"&productName&"<br>产品描述:"&productdescribe&"<br>产品价格:"&productPrice&"<br>产品提交时间:"&recordTime)
' response.Write("<br><font color='red'>产品添加成功<font>")
set rs=nothing
set conn=nothing
if err<>0 then
response.Write("<br><font color='red'>产品添加失败<font>")
else
response.Write("<br><font color='green'>产品添加成功<font>")
end if
%>
</body>
</html>
产品查询的脚本select.asp的代码:
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
dbpath=Server.MapPath("test.mdb")
conn.Open dbpath
set rs = Server.CreateObject("ADODB.recordset")
sql="SELECT * FROM chanpin"
rs.Open sql, conn
%>
<table border="1" width="100%">
<tr>
<%for each x in rs.Fields
response.write("<th>" & x.name & "</th>")
next%>
</tr>
<%do until rs.EOF%>
<tr>
<%for each x in rs.Fields%>
<td><%Response.Write(x.value)%></td>
<%next
rs.MoveNext%>
</tr>
<%loop
rs.close
conn.close
%>