xml|程式|互連網 前面我們已經介紹了使用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