網(wǎng)絡數(shù)據(jù)庫的應用是WWW上一個很重要的組成部分,可以這樣說,如果缺少了數(shù)據(jù)庫,網(wǎng)絡也就失去了靈魂。大家可以想象一下,如果沒有象YAHOO,SOHU等搜索引擎的話,那么在網(wǎng)上尋找一個目標變得多么困難,大家在茫茫網(wǎng)海里變得不知所往,動輒迷失方向。其實,這些搜索引擎是網(wǎng)絡數(shù)據(jù)庫的最典型的應用,在ASP(Active Server Pages)技術(shù)里對數(shù)據(jù)庫的直接操作是比較多的,下面講述一種由文本文件向數(shù)據(jù)庫文件傳遞數(shù)據(jù)的方法。
這個文本文體是由終端采集傳送到服務器的一個固定目錄下,由服務器去讀取數(shù)據(jù)并存放在本機數(shù)據(jù)庫里,轉(zhuǎn)換完后刪除掉這個文本文件。這樣終端負責采集數(shù)據(jù),并按給定的格式上傳到服務器的指定目錄下,服務器端的數(shù)據(jù)庫對終端來講是完全不透明的,保障了服務器的安全,對現(xiàn)在的網(wǎng)絡數(shù)據(jù)庫應用來講或許有點幫助。
Txttolib.asp
html>
head>
meta http-equiv="refresh" content="30;url=deltext.asp">
meta http-equiv="Content-Type"
content="text/html; charset=gb_2312-80">
meta name="GENERATOR" content="Microsoft FrontPage Express 2.0">
title>[把文本文件轉(zhuǎn)換成數(shù)據(jù)庫文件]/title>
/head>
body bgcolor="#FFFFFF">
align="center">
p align="center">font color="#FF0000">正在處理數(shù)據(jù),請稍候!
br>
/font>font color="#000000">align="center">%
str=server.mappath("/") ‘取得服務器的根目錄
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(str "\thetext")
Set fc = f.Files
k=1
For Each f1 in fc
file(k)=f1.name ‘得到此路徑下的所有文件名
k=k+1
next
%>% ii=1%>%
set fs = CreateObject("Scripting.FileSystemObject")%>%
while iik
response.write file(ii) "br>"
set textinstance=fs.opentextfile(str "\thetext\&; file(ii),1,false,false)‘打開文件來讀數(shù)據(jù)
while textinstance.atendofstream> true ‘如果文件沒有結(jié)束
visitornum=textinstance.readline ‘讀進一行數(shù)據(jù)
j=0
cd=len(visitornum)
for i=1 to cd
if mid(visitornum,i,1)="," then ‘數(shù)據(jù)與數(shù)據(jù)之間以“,”隔開
j=j+1
else
select case j‘分別取得各數(shù)據(jù)值
case 0
me1=me1+mid(visitornum,i,1)
case 1
me2=me2+mid(visitornum,i,1)
case 2
me3=me3+mid(visitornum,i,1)
case 3
me4=me4+mid(visitornum,i,1)
case 4
me5=me5+mid(visitornum,i,1)
case 5
me6=me6+mid(visitornum,i,1)
end select
end if
next
response.write me1 " " me2 " " me3 " " me4 " " me5 " " me6 " " "br>"
set cn=server.createobject("adodb.connection")
cn.open "water","",""
set rs=server.createobject("adodb.recordset")
sql="select * from watertable"
rs.open sql,cn,3,3‘打開數(shù)據(jù)庫進行追加操作
if rs.eof=true then
on error resume next
'rs.movelast
rs.movefirst
on error resume next
end if
rs.addnew‘增加一條記錄
rs("wvalue")=me1
rs("wdate")=me2
rs("wtime")=me3
rs("zxz")=me4
rs("jdh")=me5
rs("czh")=me6
rs.update‘更新數(shù)據(jù)
rs.close
me1=""
me2=""
me3=""
me4=""
me5=""
me6=""
wend
ii=ii+1
wend %>align="center">/font>font color="#FF0000">數(shù)據(jù)處理完畢!/font>
/p>
/body>
/html>
deltext.asp
html>
head>
meta http-equiv="refresh" content="30;url=txttolib.asp">
meta http-equiv="Content-Type"
content="text/html; charset=gb_2312-80">
meta name="GENERATOR" content="Microsoft FrontPage Express 2.0">
title>[刪除文件]/title>
/head>
body bgcolor="#FFFFFF">
p align="center">font color="#FF0000">正在刪除文件,請稍候!br>
/font>font color="#000000">%
dim file(50)‘定義最大文件數(shù)
str=server.mappath("/")
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(str "\thetext\&;)
Set fc = f.Files
k=1
For Each f1 in fc
file(k)=f1.name
response.write file(k) "br>"
k=k+1
next
%>% i=1
while ik
Set fso = CreateObject("Scripting.FileSystemObject")
fso.DeleteFile(str "\thetext\&; file(i))
i=i+1
wend
%>/font>font color="#FF0000">文件刪除完畢!/font>/p>
/body>
/html>
這兩個小程序在NT4上作者都調(diào)試通過。但有幾點要認清,一個是文本文件大小的規(guī)劃(文本文件的數(shù)量多少也是如此),如果數(shù)據(jù)量大,那么在轉(zhuǎn)換時的時間大小要調(diào)整;二是轉(zhuǎn)換文本文件到數(shù)據(jù)庫文件時的程序調(diào)試一定要嚴謹,如果程序有誤,在數(shù)據(jù)轉(zhuǎn)換過程中沒有全部轉(zhuǎn)換,而后又刪除了這個文本文件,則會有所損失。所以這兩點一定要注意,不能馬虎。