欢迎光临
我们一直在努力

用ASP、VB和XML建立互联网应用程序(4)

建站超值云服务器,限时71元/月

前面我们已经介绍了使用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

   dim xml as domdocument

   set xml = getdata(sxml)

    debug.print typename(xml)

   on error resume next

   set r = new recordset

   r.open xml

   if err.number <> 0 then

    msgbox err.description

    exit function

   else

    set getrecordset = r

   end if

  end function

  function executespwithreturn(sxml as string) as domdocument

   dim d as new dictionary

   dim xml as domdocument

   dim nodes as ixmldomnodelist

   dim n as ixmldomnode

   set xml = getdata(sxml)

   if xml.documentelement.nodename = "values" then

    set executespwithreturn = xml

   else

    发生错误

 

    set n = xml.selectsinglenode("response/data")

    if not n is nothing then

     msgbox n.text

     exit function

    else

     msgbox xml.xml

     exit function

    end if

   end if

  end function

  function getdata(sxml as string) as domdocument

   dim xhttp as new xmlhttp30

   xhttp.open "post", dataurl, false

   xhttp.send sxml

   debug.print xhttp.responsetext

   set getdata = xhttp.responsexml

  end function

  private sub optaction_click(index as integer)

   call dgcustomers_click

  end sub

  代码二、getdata.asp

   <%@ language=vbscript %>

   <% option explicit %>

   <%

    sub responseerror(sdescription)

    response.write "<response><data>error: " & sdescription & "</data></response>"

    response.end

   end sub

   response.contenttype="text/xml"

   dim xml

   dim commandtext

   dim returnsdata

   dim returnsvalues

   dim recordsaffected

   dim param

   dim paramname

   dim paramtype

   dim paramdirection

   dim paramsize

   dim paramvalue

   dim n

   dim nodename

   dim nodes

   dim conn

   dim sxml

   dim r

   dim cm

    创建domdocument对象

   set xml = server.createobject("msxml2.domdocument")

   xml.async = false

    装载post数据

   xml.load request

   if xml.parseerror.errorcode <> 0 then

    call responseerror("不能装载 xml信息。 描述: " & xml.parseerror.reason & "<br>行数: " & xml.parseerror.line)

   end if

    客户端必须发送一个commandtext元素

   set n = xml.selectsinglenode("command/commandtext")

   if n is nothing then

    call responseerror("missing <commandtext> parameter.")

   else

    commandtext = n.text

   end if

    客户端必须发送一个returnsdata或者returnsvalue元素

   set n = xml.selectsinglenode("command/returnsdata")

   if n is nothing then

    set n = xml.selectsinglenode("command/returnsvalues")

    if n is nothing then

     call responseerror("missing <returnsdata> or <returnsvalues> parameter.")

    else

     returnsvalues = (lcase(n.text)="true")

    end if

   else

    returnsdata=(lcase(n.text)="true")

   end if

   set cm = server.createobject("adodb.command")

   cm.commandtext = commandtext

   if instr(1, commandtext, " ", vbbinarycompare) > 0 then

    cm.commandtype=adcmdtext

   else

    cm.commandtype = adcmdstoredproc

   end if

    创建参数

   set nodes = xml.selectnodes("command/param")

   if nodes is nothing then

     如果没有参数

   elseif nodes.length = 0 then

      如果没有参数

   else

     for each param in nodes

       response.write server.htmlencode(param.xml) & "<br>"

      on error resume next

      paramname = param.selectsinglenode("name").text

      if err.number <> 0 then

       call responseerror("创建参数: 不能发现名称标签。")

      end if

      paramtype = param.selectsinglenode("type").text

      paramdirection = param.selectsinglenode("direction").text

      paramsize = param.selectsinglenode("size").text

      paramvalue = param.selectsinglenode("value").text

      if err.number <> 0 then

        call responseerror("参数名为 " & paramname & "的参数缺少必要的域")

      end if

      cm.parameters.append                    cm.createparameter(paramname,paramtype,paramdirection,paramsize,paramvalue)

      if err.number <> 0 then

       call responseerror("不能创建或添加名为 " & paramname & "的参数. " & err.description)

        response.end

      end if

     next

     on error goto 0

    end if

   打开连结

   set conn = server.createobject("adodb.connection")

   conn.mode=admodereadwrite

   conn.open application("connectionstring")

   if err.number <> 0 then

    call responseerror("连结出错: " & err.description)

    response.end

   end if

   连结command对象

  set cm.activeconnection = conn

   执行命令

  if returnsdata then

    用命令打开一个recordset

    set r = server.createobject("adodb.recordset")

    r.cursorlocation = aduseclient

    r.open cm,,adopenstatic,adlockreadonly

  else

    cm.execute recordsaffected, ,adexecutenorecords

  end if

   if err.number <> 0 then

    call responseerror("执行命令错误 " & commandtext & ": " & err.description)

    response.end

   end if

   if returnsdata then

    r.save response, adpersistxml

    if err.number <> 0 then

     call responseerror("数据集发生存储错误,在命令" & commandtext & ": " & err.description)

     response.end

    end if

   elseif returnsvalues then

    sxml = "<?xml version=""1.0"" encoding=""gb2312""?>" & vbcrlf & "<values>"

    set nodes = xml.selectnodes("command/param[direction=2]")

    for each n in nodes

     nodename = n.selectsinglenode("name").text

     sxml = sxml & "<" & nodename & ">" & cm.parameters(nodename).value & "" & "</" & nodename & ">"

     next

     sxml = sxml & "</values>"

     response.write sxml

   end if

   set cm = nothing

   conn.close

   set r = nothing

   set conn = nothing

   response.end

  %>

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 用ASP、VB和XML建立互联网应用程序(4)
分享到: 更多 (0)

相关推荐

  • 暂无文章