(第一个小时) 1:首先建立一个access数据库保存为 xhnew.mdb 表名为aa 字段如下: id 自动排序 name 文本 content 文本 xhtime 日期 默认值 now() title 文本 2:打开DW 建立文件conn.asp文件 代码如下: CODE: <% set conn=server.createobject("adodb.connection") conn.open "driver={microsoft access driver (*.mdb)};dbq="&server.mappath("xhnew.mdb") %> [Copy to clipboard]
这段语句是连接数据库的语句 set conn=server.createobject("adodb.connection")创建一个connection对象 用该对象的open方法打开数据库 driver={microsoft access driver (*.mdb)};这句语句是access的驱动 dbq="&server.mappath("xhnew.mdb")这句是数据库的路径 建立conn.asp是为了方便接下来的教学。。。 我们可以任何要用到打开数据库连接的文件里加 <!--#include file="conn.asp"-->就可以调用 是不是很方便。第一个小时就学这么多。。。当你弄明白上面的数据库还有代码后。。 就继续跟着我学吧!
(第2个小时) 现在来学把数据库的信息输出来 下面先建立一个主文件index.asp 代码如下 CODE: <!--#include file="conn.asp"--> <% exec="select * from aa order by id desc " set rs=server.createobject("adodb.recordset") rs.open exec,conn,1,1 %> <table width="628" height="24" border="1" align="center" cellpadding="1" cellspacing="0"> <% if rs.eof and rs.bof then response.write("暂时没有文章") else do while not rs.eof %> <tr> <td width="66" height="22" ><%=rs("id")%></td> <td width="66" ><%=rs("name")%></td> <td width="66" ><%=rs("content")%></td> <td width="273" ><%=rs("xhtime")%></td> <td width="53" ><%=rs("title")%></td> <td><a href="modify.asp?id=<%=rs("id")%>" target="_self">编辑</a></td> <td width="32" ><a href="del.asp?id=<%=rs("id")%>">删除</a></td> </tr> <% rs.movenext loop end if %> </table> <% rs.close set rs=nothing conn.close set conn=nothing %> <td><a href="add.asp">添加</a></td> [Copy to clipboard]
代码解释: (1)<% exec="select * from aa order by id desc " set rs=server.createobject("adodb.recordset") rs.open exec,conn,1,1 %> 上面的代码是创建一个recordset对象。。该对象是用来打开数据库中的表的。。也就是打开aa exec="select * from aa order by id desc " 这句的意思是查找表aa中所有信息并倒序排列 信息. *号表示aa中所有的信息 order by id 就是aa中id字段 desc 是倒序的意思 rs.open exec,conn,1,1 这句话的意思是用recordset对象来打开conn中的表 (2) <% if rs.eof and rs.bof then response.write("暂时没有文章") else do while not rs.eof %> 上面是if语句。。if else语句是相当重要的。。 希望一些初学者要认真的研究一下if else语句。。不可心急。基础才是硬道理 rs.eof 的意思是aa表中的最后一条记录 那么rs.bof就是第一条记录 整个语句用中文翻译就是 当aa表中的最后和最前一条信息没有的时候。。就输出暂时没有文章 或则就循环到eof最后一条信息。。 (3)<tr> <td width="66" height="22" ><%=rs("id")%></td> <td width="66" ><%=rs("name")%></td> <td width="66" ><%=rs("content")%></td> <td width="273" ><%=rs("xhtime")%></td> <td width="53" ><%=rs("title")%></td> <td><a href="modify.asp?id=<%=rs("id")%>" target="_self">编辑</a></td> <td width="32" ><a href="del.asp?id=<%=rs("id")%>">删除</a></td> </tr> 上面的代码比较容易理解。。<%=rs("id")%>这个用法是输出数据库字段id.. <a href="modify.asp?id=<%=rs("id")%>" target="_self">编辑</a>这段很重要。。 我来解释一下。。在ie窗口点 编辑 的时候。。页面会跳转到modify.asp的文件里的id 并进行相应的处理。。这里很难解释清楚。。在接下来的例子中再慢慢解说
|