Recommended: Use transaction control in ASP julyclyde (original work) The author is Microsoft China Community Star in February. When programming, transactions are often needed. The so-called transaction is a series of operations that must be successful. As long as one operation fails, all other steps must also be cancelled. For example, using ASP to develop a network hard disk system, the user registration part needs to do: transfer user information
summary
Returning XML data from Active Server Pages (ASP) Web pages is a common programming requirement. The methods used to implement this requirement vary by the version of Microsoft Internet Information Services (IIS) used to host the ASP application. The step-by-step guide in this article comes with relevant sample code that demonstrates how to return Extensible Markup Language (XML) data from an ASP page.
The following sample code creates an ASP page that returns the contents of the ActiveX Data Objects (ADO) record set in XML format. This code connects to an instance of the SQL Server PUBS sample database, and it opens the ADO record set by executing a SELECT query statement to retrieve data from the Authors table. Then, use the ADO's save function and the Write method of the ASP Response object to return this record set to the client browser in XML format.
1. Prerequisites
The following briefly lists the recommended hardware, software, network architecture and required Service Packs:
Microsoft Windows 2000 Professional, Windows 2000 Server, or Windows 2000 Advanced Server
Microsoft Internet Information Server (IIS) 5.0
or
Microsoft Windows NT 4.0 Server
Microsoft Internet Information Server (IIS) 4.0
2. Prepare a Web site
In Windows Explorer, create a folder named Xmltest under the root folder of the Web server (usually under C:InetpubWwwroot).
Right-click the newly created folder and click Properties.
On the Security tab, add the All Group and grant read and write permissions to this folder to the All Group. Click OK to accept the changes.
On the Start menu, point to Programs, point to Administrative Tools, and then click Internet Service Manager.
Under Internet Information Services, double-click to expand the entry corresponding to the local server.
Right-click the default Web site, point to New, and then click Virtual Directory. In the wizard, follow these steps:
When prompted, type XMLTest in the Virtual Directory Alias text box and click Next.
When you are prompted to type a Web site content directory, click Browse, select the newly created XMLTest directory, and then click Next.
When prompted to select access, select Read and Run the script (such as ASP). No other access is required in this case. Click Next to complete the wizard.
Double-click the default Web site.
Right-click the new virtual directory and click Properties.
On the Table of Contents tab, check if the Web site name (the name typed in step 6a) is listed in the Application Name text box below Application Settings. If not listed, click Create to create the application.
Close the Properties dialog box and IIS.
3. Windows 2000 XML sample code
On the Start menu, point to Programs, point to Attachments, and then click Notepad.
Select the following code, right-click the selection, and click Copy. In Notepad, click Paste on the Edit menu to add the following code to the file:
<%
'Very Important : Set the ContentType property of the Response object to text/xml.
Response.ContentType = text/xml
Dim cn
Dim rs
Dim xmlDoc
Set cn=Server.CreateObject(ADODB.Connection)
Set rs=Server.CreateObject(ADODB.Recordset)
'Replace the ADO Connection string attributes
'in the following line of code to point to your
'instance of SQL Server, and to specify the
'required security credentials for User ID and Password.
cn.Open Provider=SQLOLEDB.1; & _
User ID=; & _
Password=; & _
Initial Catalog=pubs; & _
Data Source=
rs.CursorLocation = 3
rs.Open Select * from Authors,cn
'Persist the Recorset in XML format to the ASP Response object.
'The constant value for adPersistXML is 1.
rs.Save Response, 1
%>
In line 20 of the code, replace with your username.
In line 21 of the code, replace with your password.
In line 23 of the code, replace with your SQL Server.
On the File menu, click Save.
In the Save in drop-down list box, browse to the Xmltest folder you created earlier. In the File Name text box, type Xmlw2k.asp, and in the Save Type drop-down box, click All Files. Finally click Save to save the file.
To view the page, start a web browser and type the HTTP location for the page in the address bar. If you save the file to the previously mentioned location, type http://<server name>/Xmltest/Xmlw2k.asp in the address bar.
4. Windows NT 4.0 XML sample code
On the Start menu, point to Programs, point to Attachments, and then click Notepad.
Select the following code, right-click the selection, and click Copy. In Notepad, click Paste on the Edit menu to add the following code to the file:
<%
'Very Important : Set the ContentType property of
'the Response object to text/xml.
Response.ContentType = text/xml
Dim cn
Dim rs
Dim xmlDoc
Set cn=Server.CreateObject(ADODB.Connection)
Set rs=Server.CreateObject(ADODB.Recordset)
'Replace the ADO Connection string attributes
'in the following line of code to point to your
'instance of SQL Server, and to specify the
'required security credentials for User ID and Password.
cn.Open Provider=SQLOLEDB.1; & _
User ID=; & _
Password=
Initial Catalog=pubs; & _
Data Source=
rs.CursorLocation = 3
rs.Open Select * from Authors,cn
Set xmlDoc = Server.CreateObject(Microsoft.XMLDOM)
'Persist the Recorset in XML format to the DOMDocument object.
'The constant value for adPersistXML is 1.
rs.Save xmlDoc,1
rs.Close
cn.Close
Set rs = Nothing
Set cn = Nothing
'Write out the xml property of the DOMDocument
'object to the client Browser
Response.Write xmldoc.xml
%>
In line 20 of the code, replace with your username.
In line 21 of the code, replace with your password.
In line 23 of the code, replace with your SQL Server.
On the File menu, click Save.
In the Save in drop-down list box, browse to the Xmltest folder you created earlier. In the File Name text box, type Xmlnt4.asp, and in the Save Type drop-down box, click All Files. Finally click Save to save the file.
Share: ASP script loop statement This article teaches you the ASP script loop statement: The characteristic of the ASP dynamic server page environment is that it is written through one or several scripting languages. The scripting language can be regarded as a simplified version of the programming language. It is easy to learn and master, which provides considerable convenience to the designers of dynamic websites. It can be said that the proper use of scripting language is directly related to ASP
2 pages in total Previous page 12 Next page