Use ASP, VB and XML to build Internet Applications (3)

Source: Internet
Author: User
Tags define contains join sql query requires client
xml| Program | Internet hypothesis in the example we mentioned earlier, we wanted to display the list of customers ' names in the left half of the display area in the application, and then add two links to each customer's name: Purchase history and recent Purchase. When the user clicks on one of the links, the client runs a stored procedure and displays the results in the right area. To show the flexibility of this idea, I want to have the three units of action that are used to return data perform different work processes, all of which call getdata.asp. First, run a stored procedure by calling CustOrderHist to return to the customer's purchase History, which searches the Northwind database (for convenience I use the database that comes with MS SQL) and returns a DataSet. The query that returns recent Purchase runs a stored procedure called Recentpurchasebycustomerid that receives the input CustomerID parameters and returns the name of the most recent customer purchase through the ProductName parameter. The corresponding SQL statement defining its processing is as follows:

CREATE PROCEDURE recentpurchasebycustomerid @CustomerID nchar (5), @ProductName nchar () output as SELECT @ProductName = ( SELECT top 1 ProductName from the INNER join ([order Details] INNER join Orders on Orders.orderid=[order Details]. OrderID) on products.productid = [order Details]. ProductID WHERE orders.orderdate = (SELECT MAX (orders.orderdate) from Orders
where customerid= @CustomerID) and orders.customerid= @CustomerID) go

Whether your query contains a dynamic SQL statement or a stored procedure that returns a recordset or outputs a return value, the method for handling post messages is the same:

Set xhttp = CreateObject ("MSXML2. XMLHTTP ")
Xhttp.open "POST", "http://localhost/myWeb/getData.asp", False
Xhttp.send s

Okay, now let's take a look at how to send and receive data

The XML information for the client is comprised of a <command> element and some child elements: <commandtext> element contains the name of the stored procedure, and the <returnsdata> element tells the server whether the client requires the return data to be received, and the <param> element contains parameter information. If you don't use parameters, the simplest way to send a string query is as follows:

<command>
<commandtext>

Stored procedures or dynamic SQL statements

</commandtext>
<returnsvalues> True </returnsvalues>
</command>

You can add parameters by using an <param> element for each parameter. Each <param> element has five child elements: Name,type,direction,size and value. The order of the child elements can be exchanged at will, but all elements should be indispensable, and I usually define them in the order in which they define the values of an ADO object. For example, the CustOrderHist stored procedure requires a customid parameter, so the code used to create the XML string sent to GetData.asp is:

Dim s
s = "<?xml version=" "1.0" "? >" & vbCrLf
s = S & "<command> <commandtext>"
s = S & "CustOrderHist"
s = S & "</commandtext>"
s = S & "<returnsdata> &true </returnsdata>"
s = S & "<param>"
s = S & "<name> CustomerID </name>"
s = S & "<type> <%=adVarChar%> </type>"
s = S & "<direction>" & <%=adParamInput%> </direction> "
s = S & "<size>" & Len (CustomerID) & "</size>"
s = S & "<value>" & CustomerID & "</value>"
s = S & "</param>"
s = S & "</command>"

Note that the previous code is client-side, and the ADO constants are not defined by the client-that's why they surround themselves with <%%> tags. The server replaces them with the correct values before sending the response. The GetData.asp page has a Response.ContentType whose property is "Text/xml" so that you can use the Responsexml property to return the result. When the request returns a record, you can create a Recordset object and use XMLHTTP to open it:

Dim R
Set R = CreateObject ("ADODB.") Recordset ")
R.open Xhttp.responsexml

When the query statement returns data, create a DOMDocument by setting the Responsexml property of the XMLHttpRequest object:

Dim XML
Set xml = Xhttp.responsexml

Each return value of an output parameter's XML string contains an element, which is a direct child element of the root element <values>, for example:

<?xml version= "1.0" "encoding=" "gb2312" "?
<values>
<paramname> value </paramname>
<paramname> value </paramname>
</values>

If your data is in another country's text, you may want to replace the encoding attribute with the corresponding encoding, for example, for most European languages, you can use the Iso-8859-1

The client page uses the returned data to format an HTML string for display, such as:

document.all ("Details"). InnerHTML = "Some formatted HTML string"



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.