ASP存取Oracle大文本

2010-08-28 10:47:45來源:西部e網(wǎng)作者:

      在我開發(fā)BS結(jié)構(gòu)程序中,由于經(jīng)常需要在ORACLE中處理一些容量較大的文本數(shù)據(jù),所以經(jīng)過反復(fù)測(cè)試終于用ASP成功解決了大文本數(shù)據(jù)在ORACLE下存取問題。
  
  一、運(yùn)行環(huán)境:
  1、Microsoft Windows 2000 Server + IIS 5.0
2、Oracle8i中文標(biāo)準(zhǔn)版
  
  二、建立數(shù)據(jù)表:
  CREATE TABLE SYSTEM.TEST(
BLOB LONG,
ID NUMBER)
/

  三、源程序:
1、數(shù)據(jù)存入程序:test.asp

<%
'表單提交處理部分
'--------------------------------------------------

If request("ok")=1 then

'字符轉(zhuǎn)換函數(shù)
function tansstr(sstr)
sstr=replace(sstr," "," ")
sstr=replace(sstr,chr(13) & chr(10),"<br>")
tansstr=sstr
end function

'提交數(shù)據(jù)賦值
a=lenb(request("text"))
b=len(request("text"))
c=tansstr(request("text"))

'打開數(shù)據(jù)庫(kù),打開test數(shù)據(jù)表以Rs為記錄集
Set OraSession=CreateObject("OracleInProcServer.XOraSession")
Set OraDatabase=OraSession.DbOpenDatabase("autop","system/manager",0)
Set rs=OraDatabase.CreateDynaset("select * from test order by id desc",0)

'求ID值
if rs.eof then
id=1
else
id=rs("id")+1
end if

'因?yàn)槭躍QL語(yǔ)句長(zhǎng)度大小限制所以,以非SQL語(yǔ)句存入數(shù)據(jù)
'--------------------------------------------------------

'新建記錄
rs.DbAddNew

'經(jīng)典就在本句:以RS記錄集的Fields對(duì)象的DbAppendChunk方法處理大字段存入問題。
rs.Fields("blob").DbAppendChunk(c)

'存入ID值
rs("id")=id

'刷新記錄集
rs.DbUpdate


'顯示結(jié)果部分
'---------------------------------------------------------
Response.write "數(shù)據(jù)已經(jīng)存入數(shù)據(jù)庫(kù)中。<br>"
Response.write "總計(jì)占用字符數(shù): <font color=blue>" & formatnumber(b,2,-2,-2,-1) & "</font> 字<br>"
Response.write "總計(jì)占用字節(jié)數(shù): <font color=blue>" & formatnumber(a,2,-2,-2,-1) & "</font> Byte<br>"
Response.write "<a href='view.asp'>請(qǐng)調(diào)閱……</a>"

'關(guān)閉數(shù)據(jù)連接。
rs.close
set rs=nothing
Set OraSession=nothing
Response.end
End If
%>
<html>
<body>
<form method="POST" action="test.asp">
<p><font color="#FF0000"><b>Oracle大字段在ASP中存取問題的解決:</b></font></p>
<p><textarea rows="13" name="text" cols="104"></textarea></p>
<p><input type="submit" value="存入" name="B1"></p>
<input type="hidden" name="ok" value="1">
</form>
</body>
</html>



  2、數(shù)據(jù)調(diào)出程序:view.asp
<%

'連接數(shù)據(jù)庫(kù),以只讀方式打開數(shù)據(jù)表
Set OraSession=CreateObject("OracleInProcServer.XOraSession")
Set OraDatabase=OraSession.DbOpenDatabase("autop","system/manager",0)
Set Rs=OraDatabase.DbCreateDynaset("select * from test order by id desc",4)

'賦初值:定義每次截取字節(jié)大小為1024byte,最大可以設(shè)為65280byte (64K)
Size=65280
I=0

Do
'以Rs記錄集的Fields對(duì)象的DbGetChunk方法在循環(huán)中讀出數(shù)據(jù)
Text=Rs.Fields("Blob").DbGetChunk(I*Size,Size)

Response.write Text

'求出每次取出數(shù)據(jù)的詳細(xì)字節(jié)數(shù)
Text_Size=Lenb(Text)

I=I+1

'如果每次取出數(shù)據(jù)的詳細(xì)字節(jié)數(shù)小于欲定義的截取字節(jié)大小則說明該條數(shù)據(jù)已經(jīng)完畢,退出循環(huán)。
Loop until Text_Size<Size

'關(guān)閉數(shù)據(jù)連接
Set OraSession=nothing

%>

關(guān)鍵詞:ASPOracle