共用方式為


在範例 ASP 應用程式中使用 Updategram (SQLXML 4.0)

這個 Active Server Pages (ASP) 應用程式可讓您在 Microsoft SQL Server 之 AdventureWorks2008R2 範例資料庫的 Person.Person 資料表中更新客戶資訊。此應用程式會進行下列作業:

  • 要求使用者輸入商業實體識別碼。

  • 使用這個識別碼值來執行範本,以便從 Person.Person 資料表中擷取連絡資訊。

  • 使用 HTML 表單來顯示這項資訊。

然後,使用者就可以更新連絡資訊,但無法更新商業實體識別碼 (因為 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)。請將它儲存在與您使用網際網路服務管理員公用程式所建立之虛擬根目錄相關聯的目錄中 (這個虛擬根目錄並非使用 IIS Virtual Directory Management for SQL Server 公用程式所建立,因為 IIS Virtual Directory Management for SQL Server 無法存取或識別 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>