ASP直接读取修改Access数据的演示实例

发表于:2007-06-30来源:作者:点击数: 标签:
适合初学ASP的读者的一篇不错的实例文章,具体代码如下:(把下面代码保存为1.asp) % Response.Expires=0 Set Conn=Server.CreateObject("ADODB.Connection") Conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" server.MapPath("mdb/demo.mdb") ";M

  适合初学ASP的读者的一篇不错的实例文章,具体代码如下:(把下面代码保存为1.asp)

<%
Response.Expires=0
Set Conn=Server.CreateObject("ADODB.Connection")
Conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & server.MapPath("mdb/demo.mdb") & ";Mode=ReadWrite|Share Deny None;Persist Security Info=False"
%>
<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style>
<!--
.p1 { FONT-SIZE: 9pt}
.l1 { BORDER: #000080 2px solid}
-->
</style>
</HEAD>
<BODY>
<TABLE cellPadding=0 cellSpacing=10 class=l1>
<TR><TD>
<TABLE class=p1>
<%
dim sql
dim i
dim outstr
  If Request.ServerVariables("Request_Method")="GET" then
    sql="select * from admin"
    Set RS=Server.CreateObject("ADODB.RecordSet")
    RS.Open sql,conn,3,2
    outstr="<FORM name=forma action=1.asp method=post>"
    outstr=outstr & "<TR><TD align=middle>帐号</TD><TD align=middle>密码</TD></TR>"
    for i=1 to 3
      if i<=RS.RecordCount then
        outstr=outstr & "<TR><TD><INPUT class=p1 name=t1" & i & " size=20 value=" & chr(34) & RS("帐号") & chr(34) & "></TD><TD><INPUT class=p1 name=t2" & i & " size=20 value=" & chr(34) & RS("密码") & chr(34) & "></TD></TR>"
        RS.MoveNext
      else
        outstr=outstr & "<TR><TD><INPUT class=p1 name=t1" & i & " size=20></TD><TD><INPUT class=p1 name=t2" & i & " size=20></TD></TR>"
      end if
    next
    Response.Write(outstr)
%>
<TR><TD align=middle colSpan=2><A href="javascript:document.forma.submit();">修改数据</A></TD></TR>
<%
    Conn.Close
    Set Conn=Nothing
  else
    Set RS=Conn.Execute("delete * from admin")
    for i=1 to 3
      sql="insert into admin (帐号,密码) values(@#" & Request.Form("t1" & i) & "@#,@#" & Request.Form("t2" & i) & "@#)"
      Set RS=Conn.Execute(sql)
    next
%>
<TR><TD align=middle class=p5>修改操作成功<BR><BR><A href="1.asp">显示数据</A></TD></TR>
<%   
    Conn.Close
    Set Conn=Nothing
  end if
%>
</TABLE>
</TD></TR></TABLE>
</BODY>
</HTML>

  最后给大家提供一下文章中所用到的数据库:(注意:把下载数据库文件改名为demo.mdb放到mdb目录中)

原文转自:http://www.ltesting.net