xml| Program | Internet |xml| Internet use ASP, VB and XML to build applications running on the Internet (1)
The era of using stand-alone applications on PCs is about to pass, and most applications now develop web versions or most need to share rich data resources on the Web. Although we have written for a long time based on client/server applications, most of these programs only run in small LANs. However, there are many objective reasons that require us to modify these programs so that they can run on an intranet or even the Internet.
What is the reason that compels us to do it? First of all, as the size of an enterprise gradually expands, the company may cross regional or even multinational operations, the number of employees in each branch will increase every year, these staff in the field certainly need to visit the company's database resources frequently; second, centralize the application's data resources, Enables you to better monitor the access and use of your database. Third, you can maintain and update the global application settings by getting them from a centralized location, ultimately reducing application updates. Four, try to access the database from the Web server rather than accessing the database from the client, so that you can avoid the security risks by sending the login information and the client password over the network, and by using the browser to get the data from the background, you can avoid refreshing the entire page.
This requires us to create an application running on the Internet, and if we want to create a VB program that runs on the HTTP protocol, the key is to use XML and XMLHttpRequest objects. This object is Microsoft Part of the XML parser (Msxml.dll), XMLHttpRequest object allows you to send a GET and POST request to a remote server via HTTP, and a program running on a remote server receives the request, translates its contents, returns the data or an error page to the application that invoked it. Some of the research on network programming friends will see that this idea is very much like soap, but I don't want to use soap here, because if that makes the program very complicated.
It is not realistic to want to change a fully independent client-side program, but even so, downloading application settings from a centralized server is more independent and flexible than using a local INI file or a Windows registration label. For example, suppose you have a mobile phone sales force, they need to access centralized information to more efficient sales of mobile phones, each day, the head office collection of data, and then sent to the sales staff in the form of e-mail. However, market pressures and rapidly changing forms of sales are bound to enable salespeople to access the latest data information. However, network administrators insist that salespeople at remote clients are denied access to the corporate database server because they do not want to send their username and logon password over a public network. Therefore, it is bound to use a new technology to replace the client/server based technology, do not worry, I want to read this article you will solve the problem.
Let's analyze the client/server application first. In a standard client/server application, you can initialize the database connection string at the beginning of the application, which means that the customer has the right to use the database connection string, which includes the username and password. But if you do not allow this information to be sent on the network, you must send the data directly from the client to the customer without joining the database. One solution, then, is to create an ASP page on the server (called getdata.asp in this case) to receive post data in a specific format that requires an XML string to be used to create an ADO object and run a stored procedure or dynamic SQL Statement command. If the information is valid, getdata.asp executes the stored procedure and returns an XML-formatted dataset, a return value list, or an XML string of error page information. For commands that return data, the client either instantiates or returns a value or uses an error page formatted with the XML DOM (Document Object model).
OK, let's discuss how to implement this page.
The GetData.asp page first uses a DOMDocument object to hold the data sent by the client:
' Create a DOMDocument object
Set XML = Server.CreateObject ("MSXML2.") DOMDocument ")
Xml.async = False
It then loads the post data
' Load post data
Xml. Load Request
If xml.parseError.errorCode <> 0 Then
Call Responseerror ("Cannot load XML information.") "&" Description: "& Xml.parseError.reason &" <br> Line: "& Xml.parseError.Line)
End If
It can return CommandText element values and Returndata or returnvalue element values. I'll just give you the code to return the value of the CommandText element, and the rest of the code refer to the source program that I attached below.
Set N = Xml.selectsinglenode ("Command/commandtext")
If N is nothing Then
Call Responseerror ("missing <sp_name> parameter.) ")
Else Sp_name = N.text
End If
Next, you should have the page create a command object, read all the <param> elements, and create a parameter for each element in the request. Finally, let the page open a connection use the stored procedure adexecutenorecords option to execute the request.
Set conn = Server.CreateObject ("ADODB. Connection ")
Conn. Mode=admodereadwrite
Conn.Open Application ("ConnectionString")
Set cm. Activeconnection=conn
' Return data
If not Returnsdata then
Cm. Execute
Else
Set R = Server. CreateObject ("ADODB.") Recordset ")
R.cursorlocation = adUseClient
R.open cm, adOpenStatic, adlockreadonly
End If
If the data can be returned, then the Returndata variable is true and the result dataset is returned to the client, which is still an XML document.
If Returnsdata Then
R.save Response, adPersistXML
If Err.Number <> 0 Then
Call Responseerror ("DataSet has stored Error" & "in command" & CommandText & "':" & Err.Description)
Response.End
End If
If the output parameter returns a value, the page returns an XML string containing the values. The document's root element is a <values> tag, each return value has its corresponding child element, and if any errors occur, the page formats and returns an XML string containing the error message:
Sub Responseerror (sdescription)
Response.Write "<response> <data> Error:" & sdescription & "</data> </response>"
Response.End
End Sub
Let's say 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