為什么有的留言簿不需要數(shù)據(jù)庫(kù)?
< html>
< body>
< form action="manage.asp" method="post">
< input type="text" size="10" name="username">
< input type="text" size="10" name="homepage">
< input type="text" size="10" name="Email">
< /form>
< /body>
< /html>
manage.asp
< %
strName = Request.Form("username")
strHomePage = Request.Form("homepage")
strEmail = Request.Form("Email")
' 獲取表單信息.
Set fso = Server.CreateObject("Scripting.FileSystemObject")
' 創(chuàng)建fso對(duì)象.
path = "c:chunfeng/form.txt"
ForReading = 1, ForWriting = 2, ForAppending = 3
set file = fso.opentextfile(path, ForAppending, trUE)
' 打開(kāi)文件. OpenTextFile方法將返回一個(gè)TextStream對(duì)象, 這個(gè)對(duì)象揭示了操作文件內(nèi)容的方法,如寫(xiě)、讀一行、跳過(guò)一行.另我們還在OpentextFile的命令參數(shù)中定義了trUE,意為如文件不存在,就創(chuàng)建它.因?yàn)槿绻募淮嬖?/SPAN>,且沒(méi)定義trUE參數(shù),系統(tǒng)就會(huì)出錯(cuò).
file.write(strName) & vbcrlf
' VB常量vbcrlf產(chǎn)生一個(gè)換行符.
file.write(strHomePage) & vbcrlf
file.write(strEmail) & vbcrlf
' 向文件寫(xiě)信息.
file.close
set file = nothing
set fso = nothing
' 清空.
現(xiàn)在打開(kāi)form.txt,可以看到如下的信息:
User's name
User's home page
User's email
我們?cè)侔衙恳幻麃?lái)訪者的信息從記錄中分離出來(lái)。因?yàn)樗鼪](méi)有象數(shù)據(jù)庫(kù)一樣的列,所以要想辦法從剛才記錄的文件中讀出。在所創(chuàng)建的文件中,第1行是用戶(hù)名,第2行是用戶(hù)主頁(yè),第3行是用戶(hù)電子信箱,其他用戶(hù)的信息也是這樣排列,即每3行包含一個(gè)用戶(hù)信息。這樣,我們就可以再利用FSO來(lái)讀取了:
< %
set fso = Server.Createobject("Scripting.FileSystemObject")
' 創(chuàng)建fso對(duì)象.
path = "c:chunfeng/form.txt"
set file = fso.opentextfile(path, 1)
' 打開(kāi)文件.
do until file.AtEndOfStream
' AtEndOfStream是TextStream對(duì)象的屬性,它提示何時(shí)到文件尾部.
Response.write("Name: " & file.ReadLine & " ")
' ReadLine方法讀取1行內(nèi)容,直到遇到換行符,隨后的ReadLine調(diào)用讀取下一行.
Response.write("Home Page: " & file.ReadLine & " ")
Response.write("Email: " & file.ReadLine & "< p>")
' 逐行分析并格式化數(shù)據(jù).
loop
' 循環(huán)列出每一用戶(hù)的信息.
file.close
set file = nothing
set fso = nothing
%>
都做好了。
如果form.txt本身數(shù)據(jù)出現(xiàn)差錯(cuò),如每一用戶(hù)信息只有2行而不是3行,那么就會(huì)產(chǎn)生如下錯(cuò)誤信息:
Server object error 'ASP 0177 : 800a003e'
[1]
相關(guān)文章
如何實(shí)現(xiàn)電子郵件的自動(dòng)發(fā)送?
如何實(shí)現(xiàn)電子郵件的自動(dòng)發(fā)送?...2006-11-11如何實(shí)現(xiàn)某些頁(yè)面只讓特定的用戶(hù)瀏覽?
如何實(shí)現(xiàn)某些頁(yè)面只讓特定的用戶(hù)瀏覽?...2006-11-11