Share via


예제 ASP 응용 프로그램에서 Updategram 사용(SQLXML 4.0)

이 ASP(Active Server Pages) 응용 프로그램을 사용하여 Microsoft SQL Server에서 AdventureWorks 예제 데이터베이스의 Person.Contact 테이블에 있는 고객 정보를 업데이트할 수 있습니다. 이 응용 프로그램에서는 다음 작업을 수행합니다.

  • 사용자에게 연락처 ID를 입력하도록 요청합니다.

  • 이 고객 ID로 템플릿을 실행하여 Person Contact 테이블에서 연락처 정보를 검색합니다.

  • 이 정보를 HTML 양식으로 표시합니다.

그런 다음 사용자가 연락처 정보를 업데이트할 수 있습니다. 그러나 ContactID가 기본 키이므로 연락처 ID는 업데이트할 수 없습니다. 사용자가 정보를 제출하면 Updategram이 실행되고 모든 폼 매개 변수가 Updategram에 전달됩니다.

다음은 첫 번째 템플릿(GetContact.xml)입니다. 이 템플릿을 template 유형의 가상 이름과 연관된 디렉터리에 저장합니다.

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

다음은 두 번째 템플릿(UpdateContact.xml)입니다. 이 템플릿을 template 유형의 가상 이름과 연관된 디렉터리에 저장합니다.

<ROOT xmlns:updg="urn:schemas-microsoft-com:xml-updategram">
<updg:header>
   <updg:param name="cid"/>
   <updg:param name="title" />
   <updg:param name="firstname" />
   <updg:param name="lastname" />
   <updg:param name="emailaddress" />
   <updg:param name="phone" />
</updg:header>
<updg:sync >
   <updg:before>
      <Person.Contact ContactID="$cid" /> 
   </updg:before>
   <updg:after>
      <Person.Contact ContactID="$cid" 
       Title="$title"
       FirstName="$firstname"
       LastName="$lastname"
       EmailAddress="$emailaddress"
       Phone="$phone"/>
   </updg:after>
</updg:sync>
</ROOT>

다음 코드는 ASP 응용 프로그램(SampleASP.asp)입니다. 인터넷 서비스 관리자 유틸리티를 사용하여 만드는 가상 루트에 연결된 디렉터리에 이 응용 프로그램을 저장합니다. IIS Virtual Directory Management for SQL Server에서는 ASP 응용 프로그램 식별하거나 액세스할 수 없으므로 이 가상 루트를 만들 때 IIS Virtual Directory Management for SQL Server 유틸리티를 사용하지 않습니다.

[!참고]

코드에서 "ServerName"을 Microsoft 인터넷 정보 서비스(IIS)를 실행 중인 서버의 이름으로 바꾸어야 합니다.

<% LANGUAGE=VBSCRIPT %>
<%
  Dim ContactID
  ContactID=Request.Form("cid")
%>
<html>
<body>
<%
  'If a ContactID value is not yet provided, display this form.
  if ContactID="" then
%>
<!-- If the ContactID has not been specified, display the form that allows users to enter an ID. -->
<form action="AdventureWorksContacts.asp" method="POST">
<br>
Enter ContactID: <input type=text name="cid"><br>
<input type=submit value="Submit this ID" ><br><br>
<-- Otherwise, if a ContactID is entered, display the second part of the form where the user can change customer information. -->
<%
  else
%>
<form name="Contacts" action="https://localhost/AdventureWorks/Template/UpdateContact.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/AdventureWorks/Template/GetContact.xml?cid=" & ContactID)
    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 "Contact ID: <input type=text readonly=true style='background-color:silver' name=cid value="""
  Response.Write objCustomer.attributes(0).value
  Response.Write """><br><br>"


  Response.Write "Title: <input type=text name=title 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>"

  Response.Write "Email Address: <input type=text name=emailaddress value="""
  Response.Write objCustomer.attributes(4).value
  Response.Write """><br><br>"

  Response.Write "Phone: <input type=text name=phone value="""
  Response.Write objCustomer.attributes(9).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="<%=ContactID%>"><br><br>
<% end if %>

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