ASP对Access数据库的连接、增删改查及ASP的基本语法

    <html>  
    <head>  
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
    <title>数据库的连接、增删改查</title>  
    </head>  
    <body>  
    <%  
    db="Database.mdb"  
    Set conn = Server.CreateObject("ADODB.Connection")  
    conn.Open "driver={Microsoft Access Driver (*.mdb)};pwd=admin;dbq=" & Server.MapPath(db)   
    response.write "数据库连接成功!"  
    Set rs = Server.CreateObject( "ADODB.Recordset" )  
    sql = "select * from test;"  
    rs.open sql,conn,1,3  
    %>  
    <br/>  
    表test的内容:  
    <table border="1">  
      <tr>  
        <td>id</td>  
        <td>username</td>  
        <td>password</td>  
      </tr>  
      <%  
    if (rs.bof and rs.eof) then  
    response.write "nodata!"  
    else  
    do while not rs.eof  
    %>  
      <tr>  
        <td><%=rs("id")%></td>  
        <td><%=rs("username")%></td>  
        <td><%=rs("password")%></td>  
      </tr>  
      <%  
    rs.movenext  
    loop  
    end if  
    %>  
    </table>  
    <%  
    'conn.execute "update test set username='2' where username='a';"  
    conn.execute "insert into test(username,password) values ('3','a');"  
    %>  
    ---------插入数据之后--------<br/>  
    表test的内容:<br/>  
    <%  
    Set rs = Server.CreateObject( "ADODB.Recordset" )  
    sql = "select * from test;"  
    rs.open sql,conn,1,3  
    %>  
    <table border="1">  
      <tr>  
        <td>id</td>  
        <td>username</td>  
        <td>password</td>  
      </tr>  
      <%  
    if (rs.bof and rs.eof) then  
    response.write "nodata!"  
    else  
    do while not rs.eof  
    %>  
      <tr>  
        <td><%=rs("id")%></td>  
        <td><%=rs("username")%></td>  
        <td><%=rs("password")%></td>  
      </tr>  
      <%  
    rs.movenext  
    loop  
    end if  
    %>  
    </table>  
    <%  
    rs.close  
    set rs=nothing  
    conn.close  
    set conn=nothing  
    %>  
    </body>  
    </html>  

这段代码是用记事本敲出来的,没有用任何开发工具,所有缺乏缩进等基本代码格式见谅,ASP实在缺乏代码开发工具。

ASPEDIT都是一些1997年就出版的东西了。

1、<head>部分

[html] view plain copy
print?

    <head>  
    <!--必须声明使用utf-8编码,否则在IE8中页面显示会乱码-->  
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
    <title>数据库的连接、增删改查</title>  
    </head>  

以下就是<body>部分了

2、连接数据库部分

[html] view plain copy
print?

    <%  
    'asp同使用'去注释,不允许使用;来结束语句,每一个enter键就是代表一条语句结束  
    'Access数据库文件是database.mdb  
    db="Database.mdb"  
    Set conn = Server.CreateObject("ADODB.Connection")  
    '指定动作,pwd后的值代表此数据库的密码  
    conn.Open "driver={Microsoft Access Driver (*.mdb)};pwd=admin;dbq=" & Server.MapPath(db)  
    '在网页上输出东西,可以用response对象   
    response.write "数据库连接成功!"  
    %>  

3、查询部分

[html] view plain copy
print?

    <%  
    '指定动作  
    Set rs = Server.CreateObject( "ADODB.Recordset" )  
    '查询语句  
    sql = "select * from test;"  
    '使用sql对conn进行查询,后面的参数代表我要完全操作这个数据库  
    rs.open sql,conn,1,3  
    %>  
    <!--此乃表头,asp代码可以与html代码混合,table默认是没有边框的,所以要设置一个border参数-->  
    <br/>  
    表test的内容:  
    <table border="1">  
      <tr>  
        <td>id</td>  
        <td>username</td>  
        <td>password</td>  
      </tr>  
    <%  
    '如果没有查到任何东西这样写,ASP没有括号,条件体必须用end if结束,then相当于左括号,end if相当于右括号  
    if (rs.bof and rs.eof) then  
    response.write "nodata!"  
    else  
    '否则就循环,直到读完这条游标,循环条件是not rs.eof,ASP没有括号,do while循环体必须用loop结束,do while后自带左括号,loop相当于右括号  
    '下面的html循环一次就向网页写入一次  
    do while not rs.eof  
    %>  
      <tr>  
        <td><%=rs("id")%></td>  
        <td><%=rs("username")%></td>  
        <td><%=rs("password")%></td>  
      </tr>  
      <%  
      '读完一项,游标向下拉  
    rs.movenext  
    loop  
    end if  
    %>  
    </table>  

4、增加、删除、修改操作部分

这里没有写出删除操作,修改操作则被注释掉了,但他们的用法与增加操作完全一样,

[html] view plain copy
print?

    <%  
    '与查询操作不同的是,插入操作很简短。  
    '修改操作可以用下面的语句,把test表的username字段(列)中为a的项都改为2  
    'conn.execute "update test set username='2' where username='a';"  
    conn.execute "insert into test(username,password) values ('3','a');"  
    %>  

插入之后再次查询的道理与上面的未插入之前是一样道理的,就不再进行说明了。
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值