注意:要運行上述ACCESS例程請先下載數據庫,放在db 或 rwdata文件夾下。請將例程放在根目錄下。 <% Dim mode mode=request.form("text4") if mode=1 then name=request.form("text1") age=request.form("text2") addr=request.form("text3") if not isnumeric(age) then response.write "age must be number!try again":response.end age=clng(age) end if '******************************************************************** ' 與AccessDB相關的連接信息 ' 可以參照它建立您的數據庫連接 '******************************************************************** Dim conn,rs,dbpath Set conn=Server.CreateObject("ADODB.Connection") Set rs=Server.CreateObject("ADODB.RecordSet") dbpath=Server.MapPath("rwdata/db_test_com.mdb") conn.Open "Driver={Microsoft Access Driver (*.mdb)}; DBQ="&dbpath '******************************************************************** '******************************************************************** dim strsql '******************************************************************** ' 讀數據庫相關操作 sub readdb() strsql="select * from test" rs.open strsql,conn,1,1 if rs.EOF then response.write "no record at all":exit sub response.write "" response.write "" for i=0 to rs.Fields.Count-1 response.write ""&rs.Fields(i).Name&" | " next response.write " " while not rs.EOF response.write "" for i=0 to rs.Fields.Count-1 response.write ""&rs.Fields(i).Value&" | " next response.write " " rs.MoveNext wend response.write " " rs.Close end sub '******************************************************************** '******************************************************************** ' 寫數據庫相關 sub insertdata() strsql="INSERT INTO test(name,age,addr) VALUES('"&name&"',"&age&",'"&addr&"')" rs.Open strsql,conn,1,3 end sub '********************************************************************
if mode=1 then call insertdata() response.write "insert ok!" elseif mode=2 then call readdb() end if '******************************************************************** ' 釋放數據庫連接對象 set rs=nothing set conn=nothing %>
|