📄 197.htm
字号:
<p>用ASP、VB和XML建立互联网应用程序(4)</p>
<p></p>
<p>前面我们已经介绍了使用ASP和XML混合编程,那是因为ASP页面能够很容易让我们看清应用程序正在做什么,但是你如</p>
<p>果你不想使用ASP的话,你也可以使用任何你熟悉的技术去创建一个客户端程序。下面,我提供了一段VB代码,它的功能和</p>
<p>ASP页面一样,也可以显示相同的数据,但是这个VB程序不会创建发送到服务器的XML字符串。它通过运行一个名叫</p>
<p>Initialize的存储过程,从服务器取回XML字符串,来查询ClientCommands表的内容。</p>
<p></p>
<p> ClientCommands表包括两个域:command_name域和command_xml域。客户端程序需要三个特定的command_name</p>
<p>域:getCustomerList,CustOrderHist和RecentPurchaseByCustomerID。每一个命令的command_xml域包括程序发送到</p>
<p>getData.asp页面的XML字符串,这样,就可以集中控制XML字符串了,就象存储过程名字所表现的意思一样,在发送XML字</p>
<p>符串到getData.asp之前,客户端程序使用XML DOM来设置存储过程的参数值。我提供的代码,包含了用于定义Initialize</p>
<p>过程和用于创建ClientCommands表的SQL语句。</p>
<p></p>
<p> 我提供的例程中还说明了如何使用XHTTPRequest对象实现我在本文一开始时许下的承诺:任何远程的机器上的应用程序</p>
<p>都可以访问getData.asp;当然,你也可以通过设置IIS和NTFS权限来限制访问ASP页面;你可以在服务器上而不是客户机上</p>
<p>存储全局应用程序设置;你可以避免通过网络发送数据库用户名和密码所带来的隐患性。还有,在IE中,应用程序可以只</p>
<p>显示需要的数据而不用刷新整个页面。</p>
<p></p>
<p> 在实际的编程过程中,你们应当使用一些方法使应用程序更加有高效性。你可以把ASP中的关于取得数据的代码端搬到</p>
<p>一个COM应用程序中去然后创建一个XSLT变换来显示返回的数据。好,我不多说了,现在你所要做的就是试一试吧!</p>
<p></p>
<p> Option Explicit</p>
<p> Private RCommands As Recordset</p>
<p> Private RCustomers As Recordset</p>
<p> Private RCust As Recordset</p>
<p> Private sCustListCommand As String</p>
<p> Private Const dataURL = "http://localhost/XHTTPRequest/getData.asp" </p>
<p> Private arrCustomerIDs() As String</p>
<p> Private Enum ActionEnum</p>
<p> VIEW_HISTORY = 0</p>
<p> VIEW_RECENT_PRODUCT = 1</p>
<p> End Enum</p>
<p></p>
<p> Private Sub dgCustomers_Click()</p>
<p> Dim CustomerID As String</p>
<p> CustomerID = RCustomers("CustomerID").Value</p>
<p> If CustomerID <> "" Then</p>
<p> If optAction(VIEW_HISTORY).Value Then</p>
<p> Call getCustomerDetail(CustomerID)</p>
<p> Else</p>
<p> Call getRecentProduct(CustomerID)</p>
<p> End If</p>
<p> End If</p>
<p> End Sub</p>
<p></p>
<p> Private Sub Form_Load()</p>
<p> Call initialize</p>
<p> Call getCustomerList</p>
<p> End Sub</p>
<p></p>
<p> Sub initialize()</p>
<p> 注释: 从数据库返回命令名和相应的值 </p>
<p></p>
<p> Dim sXML As String</p>
<p> Dim vRet As Variant</p>
<p> Dim F As Field</p>
<p> sXML = "<?xml version=""1.0""?>"</p>
<p> sXML = sXML & "<command><commandtext>Initialize</commandtext>"</p>
<p> sXML = sXML & "<returnsdata>True</returnsdata>"</p>
<p> sXML = sXML & "</command>"</p>
<p> Set RCommands = getRecordset(sXML)</p>
<p> Do While Not RCommands.EOF</p>
<p> For Each F In RCommands.Fields</p>
<p> Debug.Print F.Name & "=" & F.Value</p>
<p> Next</p>
<p> RCommands.MoveNext</p>
<p> Loop</p>
<p> End Sub</p>
<p></p>
<p> Function getCommandXML(command_name As String) As String</p>
<p> RCommands.MoveFirst</p>
<p> RCommands.Find "command_name=注释:" & command_name & "注释:", , adSearchForward, 1</p>
<p> If RCommands.EOF Then</p>
<p> MsgBox "Cannot find any command associated with the name 注释:" & command_name & "注释:."</p>
<p> Exit Function</p>
<p> Else</p>
<p> getCommandXML = RCommands("command_xml")</p>
<p> End If</p>
<p> End Function</p>
<p></p>
<p> Sub getRecentProduct(CustomerID As String)</p>
<p> Dim sXML As String</p>
<p> Dim xml As DOMDocument</p>
<p> Dim N As IXMLDOMNode</p>
<p> Dim productName As String</p>
<p> sXML = getCommandXML("RecentPurchaseByCustomerID")</p>
<p> Set xml = New DOMDocument</p>
<p> xml.loadXML sXML</p>
<p> Set N = xml.selectSingleNode("command/param[name=注释:CustomerID注释:]/value")</p>
<p> N.Text = CustomerID</p>
<p> Set xml = executeSPWithReturn(xml.xml)</p>
<p> productName = xml.selectSingleNode("values/ProductName").Text</p>
<p> 注释: 显示text域</p>
<p> txtResult.Text = ""</p>
<p> Me.txtResult.Visible = True</p>
<p> dgResult.Visible = False</p>
<p> 注释: 显示product名</p>
<p> txtResult.Text = "最近的产品是: " & productName</p>
<p> End Sub</p>
<p></p>
<p> Sub getCustomerList()</p>
<p> Dim sXML As String</p>
<p> Dim i As Integer</p>
<p> Dim s As String</p>
<p> sXML = getCommandXML("getCustomerList")</p>
<p> Set RCustomers = getRecordset(sXML)</p>
<p> Set dgCustomers.DataSource = RCustomers</p>
<p> End Sub</p>
<p></p>
<p> Sub getCustomerDetail(CustomerID As String)</p>
<p> 注释: 找出列表中相关联的ID号</p>
<p> Dim sXML As String</p>
<p> Dim R As Recordset</p>
<p> Dim F As Field</p>
<p> Dim s As String</p>
<p> Dim N As IXMLDOMNode</p>
<p> Dim xml As DOMDocument</p>
<p> sXML = getCommandXML("CustOrderHist")</p>
<p> Set xml = New DOMDocument</p>
<p> xml.loadXML sXML</p>
<p> Set N = xml.selectSingleNode("command/param[name=注释:CustomerID注释:]/value")</p>
<p> N.Text = CustomerID</p>
<p> Set R = getRecordset(xml.xml)</p>
<p> 注释: 隐藏 text , 因为它是一个记录集</p>
<p> txtResult.Visible = False</p>
<p></p>
<p> dgResult.Visible = True</p>
<p> Set dgResult.DataSource = R</p>
<p> End Sub</p>
<p></p>
<p> Function getRecordset(sXML As String) As Recordset</p>
<p> Dim R As Recordset</p>
<p> Dim xml As DOMDocument</p>
<p> Set xml = getData(sXML)</p>
<p> Debug.Print TypeName(xml)</p>
<p> On Error Resume Next</p>
<p> Set R = New Recordset</p>
<p> R.Open xml</p>
<p> If Err.Number <> 0 Then</p>
<p> MsgBox Err.Description</p>
<p> Exit Function</p>
<p> Else</p>
<p> Set getRecordset = R</p>
<p> End If</p>
<p> End Function</p>
<p></p>
<p> Function executeSPWithReturn(sXML As String) As DOMDocument</p>
<p> Dim d As New Dictionary</p>
<p> Dim xml As DOMDocument</p>
<p> Dim nodes As IXMLDOMNodeList</p>
<p> Dim N As IXMLDOMNode</p>
<p> Set xml = getData(sXML)</p>
<p> If xml.documentElement.nodeName = "values" Then</p>
<p> Set executeSPWithReturn = xml</p>
<p> Else</p>
<p> 注释:发生错误 </p>
<p> </p>
<p> Set N = xml.selectSingleNode("response/data")</p>
<p> If Not N Is Nothing Then</p>
<p> MsgBox N.Text</p>
<p> Exit Function</p>
<p> Else</p>
<p> MsgBox xml.xml</p>
<p> Exit Function</p>
<p> End If</p>
<p> End If</p>
<p> End Function</p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -