用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