用ASP、VB和XML建立互聯網應用程序(4)
2024-09-05 20:55:54
供稿:網友
前面我們已經介紹了使用asp和xml混合編程,那是因為asp頁面能夠很容易讓我們看清應用程序正在做什么,但是你如果你不想使用asp的話,你也可以使用任何你熟悉的技術去創建一個客戶端程序。下面,我提供了一段vb代碼,它的功能和asp頁面一樣,也可以顯示相同的數據,但是這個vb程序不會創建發送到服務器的xml字符串。它通過運行一個名叫initialize的存儲過程,從服務器取回xml字符串,來查詢clientcommands表的內容。
clientcommands表包括兩個域:command_name域和command_xml域。客戶端程序需要三個特定的command_name域:getcustomerlist,custorderhist和recentpurchasebycustomerid。每一個命令的command_xml域包括程序發送到getdata.asp頁面的xml字符串,這樣,就可以集中控制xml字符串了,就象存儲過程名字所表現的意思一樣,在發送xml字符串到getdata.asp之前,客戶端程序使用xml dom來設置存儲過程的參數值。我提供的代碼,包含了用于定義initialize過程和用于創建clientcommands表的sql語句。
我提供的例程中還說明了如何使用xhttprequest對象實現我在本文一開始時許下的承諾:任何遠程的機器上的應用程序都可以訪問getdata.asp;當然,你也可以通過設置iis和ntfs權限來限制訪問asp頁面;你可以在服務器上而不是客戶機上存儲全局應用程序設置;你可以避免通過網絡發送數據庫用戶名和密碼所帶來的隱患性。還有,在ie中,應用程序可以只顯示需要的數據而不用刷新整個頁面。
在實際的編程過程中,你們應當使用一些方法使應用程序更加有高效性。你可以把asp中的關于取得數據的代碼端搬到一個com應用程序中去然后創建一個xslt變換來顯示返回的數據。好,我不多說了,現在你所要做的就是試一試吧!
option explicit
private rcommands as recordset
private rcustomers as recordset
private rcust as recordset
private scustlistcommand as string
private const dataurl = "http://localhost/xhttprequest/getdata.asp"
private arrcustomerids() as string
private enum actionenum
view_history = 0
view_recent_product = 1
end enum
private sub dgcustomers_click()
dim customerid as string
customerid = rcustomers("customerid").value
if customerid <> "" then
if optaction(view_history).value then
call getcustomerdetail(customerid)
else
call getrecentproduct(customerid)
end if
end if
end sub
private sub form_load()
call initialize
call getcustomerlist
end sub
sub initialize()
' 從數據庫返回命令名和相應的值
dim sxml as string
dim vret as variant
dim f as field
sxml = "<?xml version=""1.0""?>"
sxml = sxml & "<command><commandtext>initialize</commandtext>"
sxml = sxml & "<returnsdata>true</returnsdata>"
sxml = sxml & "</command>"
set rcommands = getrecordset(sxml)
do while not rcommands.eof
for each f in rcommands.fields
debug.print f.name & "=" & f.value
next
rcommands.movenext
loop
end sub
function getcommandxml(command_name as string) as string
rcommands.movefirst
rcommands.find "command_name='" & command_name & "'", , adsearchforward, 1
if rcommands.eof then
msgbox "cannot find any command associated with the name '" & command_name & "'."
exit function
else
getcommandxml = rcommands("command_xml")
end if
end function
sub getrecentproduct(customerid as string)
dim sxml as string
dim xml as domdocument
dim n as ixmldomnode
dim productname as string
sxml = getcommandxml("recentpurchasebycustomerid")
set xml = new domdocument
xml.loadxml sxml
set n = xml.selectsinglenode("command/param[name='customerid']/value")
n.text = customerid
set xml = executespwithreturn(xml.xml)
productname = xml.selectsinglenode("values/productname").text
' 顯示text域
txtresult.text = ""
me.txtresult.visible = true
dgresult.visible = false
' 顯示product名
txtresult.text = "最近的產品是: " & productname
end sub
sub getcustomerlist()
dim sxml as string
dim i as integer
dim s as string
sxml = getcommandxml("getcustomerlist")
set rcustomers = getrecordset(sxml)
set dgcustomers.datasource = rcustomers
end sub
sub getcustomerdetail(customerid as string)
' 找出列表中相關聯的id號
dim sxml as string
dim r as recordset
dim f as field
dim s as string
dim n as ixmldomnode
dim xml as domdocument
sxml = getcommandxml("custorderhist")
set xml = new domdocument
xml.loadxml sxml
set n = xml.selectsinglenode("command/param[name='customerid']/value")
n.text = customerid
set r = getrecordset(xml.xml)
' 隱藏 text , 因為它是一個記錄集
txtresult.visible = false
dgresult.visible = true
set dgresult.datasource = r
end sub
function getrecordset(sxml as string) as recordset
dim r as recordset