如何对Oracle8数据库进行维护?
来源:asp之家 时间:2009-11-20 18:01:00
我在Web服务器端安装了Web Server IIS4.0、Oracle Net8 for Client,并创建好了和Oracle8数据库的ODBC接口。该如何编写ASP代码对它进行增加、删除、修改、保存和查询等维护工作?
首先,要在Oracle服务器上安装Oracle8数据库,(一般操作系统为UNIX),在Web服务器Windows NT的控制面版中正确配置ODBC。
假设我们的Oracle用户为user—name,密码为user—passwd,编码库表为tab—code[包含两个字段: bm(编码字段,字符型,5位)和mc(名称字段,字符型,20位)]。可进行如下操作:
1、连接数据库,定义子例程:
<% mc=Request("mc") %>
' 赋初值
<%
Set Conn=Server.CreateObject("ADODB.Connection")
' 连接源数据库
Conn.Open "infosystem","user—name","user—passwd"
set bmrs = Conn.Execute("SELECT bm FROM bmb where mc=' "&mc&" ' ")
temp1 = bmrs("bm")
sql—1 = request("sql—1")
if sql—1 = " " then
sql—1="SELECT bm,mc FROM table—code WHERE bm like ' " & temp1 & "%' order by bm"
end if
Call treat()
%>
<%
Sub Reset()
bm = " "
temp2 = " "
End Sub
%>
<%
Call Reset()
num—recn = Conn.Execute("SELECT Max(bm) FROM table—code WHERE bm like ' " & temp1 & "%' ")
%>
<%
Sub treat()
Set num—recn = Conn.Execute(sql—1)
if num—recn.eof then
else
sum—recn = 0
Do While Not num—recn.EOF
num—recn.MoveNext
sum—recn = sum—recn+1
Loop
if sum—recn <> 0 then
sele—recn=sum—recn
num—recn.MoveFirst
num—recn.Move(sum—recn-1)
Call extract()
end if
end if
End Sub
%>
<%
Sub extract()
bm = num—recn("bm")
temp2 = num—recn(1)
num—recn.Close
End Sub
%>
2、增加一条记录:
<%
sum—recn =sum—recn + 1
bm = Request("bm")
temp2 = Request("temp2")
Insertsql = "INSERT INTO table—code(bm,mc) VALUES (' " & bm & " ',' " & temp2 & " ')"
Set Insertrs = Conn.Execute(Insertsql)
%>
3、删除当前记录:
<%bm = Request("bm")
set delrs = Conn.Execute("Delete From table—code where bm=' " & bm &" ' ")
Call treat()
%>
4、更新当前记录后入库(提交更新过的记录):
<%
bm = Request("bm")
temp2 = Request("temp2")
UpdateSQL = "UPDATE table—code SET mc=' " & temp2 & " ' where bm=' " & bm &" ' "
Set UpdateRS=Conn.Execute(UpdateSQL)
%>
5、设置查询条件(内容),输出查询结果:
<%
Call Reset()
temp2 = Request("temp2")
sql—1="SELECT bm,mc FROM table—code WHERE mc like ' " & temp2 & "%' order by bm"
call treat()
%>
' 到第一条记录
<%
Set num—recn = Conn.Execute(sql—1)
sum—recn = Request("sum—recn")
sele—recn=1
Call extract()
%>
<%
' 查找前一条记录
Set num—recn = Conn.Execute(sql—1)
sele—recn = Request("sele—recn")
sum—recn = Request("sum—recn")
if sele—recn >1 then
sele—recn = sele—recn-1
num—recn.Move(sele—recn-1)
end if
Call extract()
%>
<%
' 查找下一条记录
Set num—recn = Conn.Execute(sql—1)
sele—recn = Request("sele—recn")
sum—recn = Request("sum—recn")
if CInt(sele—recn) < CInt(sum—recn) then
sele—recn=sele—recn+1
num—recn.Move(sele—recn-1)
else
num—recn.MoveFirst
num—recn.Move(sum—recn-1)
end if
Call extract()
%>
<%
' 到最后一条记录
Call treat()
%>