在示例 ASP 应用程序中使用 Updategram (SQLXML 4.0)

使用此 Active Server Pages (ASP) 应用程序,您可以更新 Microsoft SQL Server 中 AdventureWorks2008R2 示例数据库的 Person.Person 表中的客户信息。应用程序执行以下操作:

  • 请求用户输入业务实体 ID。

  • 使用此 ID 值执行模板,以便从 Person.Person 表中检索联系人信息。

  • 通过使用 HTML 表单显示此信息。

然后,用户可以更新联系人信息,但不能更新业务实体 ID(因为 BusinessEntityID 为主键)。在用户提交信息之后,将执行一个 updategram,并且所有表单参数将传递到该 updategram。

以下模板为第一个模板 (GetPerson.xml)。请将此模板保存在与 template 类型的虚拟名称关联的目录中。

<root xmlns:sql="urn:schemas-microsoft-com:xml-sql">
   <sql:header>
      <sql:param name="bid"></sql:param>
   </sql:header>
   <sql:query>
      SELECT  * 
      FROM    Person.Person
      WHERE   BusinessEntityID=@bid 
      FOR XML AUTO
   </sql:query>
</root>

以下模板为第二个模板 (UpdatePerson.xml)。请将此模板保存在与 template 类型的虚拟名称关联的目录中。

<ROOT xmlns:updg="urn:schemas-microsoft-com:xml-updategram">
<updg:header>
   <updg:param name="bid"/>
   <updg:param name="jobtitle" />
   <updg:param name="firstname" />
   <updg:param name="lastname" />
</updg:header>
<updg:sync >
   <updg:before>
      <Person.Person BusinessEntityID="$bid" /> 
   </updg:before>
   <updg:after>
      <Person.Person BusinessEntityID="$bid" 
       JobTitle="$jobtitle"
       FirstName="$firstname"
       LastName="$lastname"/>
   </updg:after>
</updg:sync>
</ROOT>

以下代码为 ASP 应用程序 (SampleASP.asp)。请将它保存在与使用 Internet 服务管理器实用工具所创建的虚拟根关联的目录中。(此虚拟根不是通过使用用于 SQL Server 的 IIS 虚拟目录管理实用工具创建的,因为用于 SQL Server 的 IIS 虚拟目录管理工具无法访问或识别 ASP 应用程序。)

注意注意

在此代码中,必须将“ServerName”替换为运行 Microsoft Internet Information Services (IIS) 的服务器的名称。

<% LANGUAGE=VBSCRIPT %>
<%
  Dim BusinessEntityID
  BusinessEntityID=Request.Form("bid")
%>
<html>
<body>
<%
  'If a BusinessEntityID value is not yet provided, display this form.
  if BusinessEntityID="" then
%>
<!-- If the BusinessEntityID has not been specified, display the form that allows users to enter an ID. -->
<form action="AdventureWorksPeople.asp" method="POST">
<br>
Enter BusinessEntityID: <input type=text name="bid"><br>
<input type=submit value="Submit this ID" ><br><br>
<-- Otherwise, if a BusinessEntityID is entered, display the second part of the form where the user can change customer information. -->
<%
  else
%>
<form name="People" action="https://localhost/AdventureWorks2008R2/Template/UpdatePerson.xml" method="POST">
You may update customer information below.<br><br>
<!-- A comment goes here to separate the parts of the application or page. -->
<br>
<%
  ' Load the document in the parser and extract the values to populate the form.
    Set objXML=Server.CreateObject("MSXML2.DomDocument")
    ObjXML.setProperty "ServerHTTPRequest", TRUE

    objXML.async=False
    objXML.Load("https://localhost/AdventureWorks2008R2/Template/GetPerson.xml?bid=" & BusinessEntityID)
    set objCustomer=objXML.documentElement.childNodes.Item(0)

  ' In retrieving data from the database, if a value in the column is NULL there
  '  is no attribute for the corresponding element. In this case,
  ' skip the error generation and go to the next attribute.

  On Error Resume Next

  Response.Write "Business Entity ID: <input type=text readonly=true style='background-color:silver' name=bid value="""
  Response.Write objCustomer.attributes(0).value
  Response.Write """><br><br>"


  Response.Write "Job Title: <input type=text name=jobtitle value="""
  Response.Write objCustomer.attributes(1).value
  Response.Write """><br><br>"

  Response.Write "First Name: <input type=text name=firstname value="""
  Response.Write objCustomer.attributes(2).value
  Response.Write """><br>"

  Response.Write "Last Name: <input type=text name=lastname value="""
  Response.Write objCustomer.attributes(3).value
  Response.Write """><br><br>"

  set objCustomer=Nothing
  Set objXML=Nothing
%>
<input type="submit" value="Submit this change" ><br><br>
<input type=hidden name="contenttype" value="text/xml">
<input type=hidden name="eeid" value="<%=BusinessEntityID%>"><br><br>
<% end if %>

</form>
</body>
</html>