<2>顯示數據庫記錄 原理:將數據庫中的記錄一一顯示到客戶端瀏覽器,依次讀出數據庫中的每一條記錄 如果是從頭到尾:用循環并判斷指針是否到末 使用: not rs.eof 如果是從尾到頭:用循環并判斷指針是否到開始 使用:not rs.bof
<!--#include file=conn.asp--> (包含conn.asp用來打開bbs/db1/目錄下的user.mdb數據庫) <% set rs=server.CreateObject("adodb.recordset") (建立recordset對象) sqlstr="select * from message" ---->(message為數據庫中的一個數據表,即你要顯示的數據所存放的數據表) rs.open sqlstr,conn,1,3 ---->(表示打開數據庫的方式) rs.movefirst ---->(將指針移到第一條記錄) while not rs.eof ---->(判斷指針是否到末尾) aspxuexi.com 整理文檔 asp學習網 response.write(rs("name")) ---->(顯示數據表message中的name字段) rs.movenext ---->(將指針移動到下一條記錄) wend ---->(循環結束) rs.close conn.close 這幾句是用來關閉數據庫 set rs=nothing set conn=nothing %> 其中response對象是服務器向客戶端瀏覽器發送的信息.
<3>增加數據庫記錄 增加數據庫記錄用到rs.addnew,rs.update兩個函數 <!--#include file=conn.asp--> (包含conn.asp用來打開bbs/db1/目錄下的user.mdb數據庫) <% set rs=server.CreateObject("adodb.recordset") (建立recordset對象) sqlstr="select * from message" ---->(message為數據庫中的一個數據表,即你要顯示的數據所存放的數據表) rs.open sqlstr,conn,1,3 ---->(表示打開數據庫的方式) rs.addnew 新增加一條記錄 rs("name")="xx" 將xx的值傳給name字段 rs.update 刷新數據庫 rs.close conn.close 這幾句是用來關閉數據庫 set rs=nothing set conn=nothing %>
<4>刪除一條記錄 刪除數據庫記錄主要用到rs.delete,rs.update <!--#include file=conn.asp--> (包含conn.asp用來打開bbs/db1/目錄下的user.mdb數據庫) <% dim name name="xx" set rs=server.CreateObject("adodb.recordset") (建立recordset對象) sqlstr="select * from message" ---->(message為數據庫中的一個數據表,即你要顯示的數據所存放的數據表) rs.open sqlstr,conn,1,3 ---->(表示打開數據庫的方式) while not rs.eof if rs.("name")=name then rs.delete rs.update 查詢數據表中的name字段的值是否等于變量name的值"xx",如果符合就執行刪除, else 否則繼續查詢,直到指針到末尾為止 rs.movenext emd if wend rs.close conn.close 這幾句是用來關閉數據庫 set rs=nothing set conn=nothing %>
<5>關于數據庫的查詢 (a) 查詢字段為字符型 <% dim user,pass,qq,mail,message user=request.Form("user") pass=request.Form("pass") qq=request.Form("qq") mail=request.Form("mail") message=request.Form("message") if trim(user)&"x"="x" or trim(pass)&"x"="x" then (檢測user值和pass值是否為空,可以檢測到空格) response.write("注冊信息不能為空") else set rs=server.CreateObject("adodb.recordset") sqlstr="select * from user where user=‘‘‘‘"&user&"‘‘‘‘" (查詢user數據表中的user字段其中user字段為字符型) rs.open sqlstr,conn,1,3 if rs.eof then rs.addnew rs("user")=user rs("pass")=pass rs("qq")=qq rs("mail")=mail rs("message")=message rs.update rs.close conn.close set rs=nothing set conn=nothing response.write("注冊成功") end if rs.close conn.close set rs=nothing set conn=nothing response.write("注冊重名") %> (b)查詢字段為數字型 <% dim num num=request.Form("num") set rs=server.CreateObject("adodb.recordset") sqlstr="select * from message where id="&num (查詢message數據表中id字段的值是否與num相等,其中id為數字型) rs.open sqlstr,conn,1,3 if not rs.eof then rs.delete rs.update rs.close conn.close set rs=nothing set conn=nothing response.write("刪除成功") end if rs.close conn.close set rs=nothing set conn=nothing response.write("刪除失敗") %>