Partilhar via


Usando um diagrama de atualização em um aplicativo ASP de exemplo (SQLXML 4.0)

Este aplicativo ASP (Active Server Pages) permite que você atualize as informações do cliente na tabela Person.Contact no banco de dados de exemplo AdventureWorks no Microsoft SQL Server. O aplicativo faz o seguinte:

  • Pede ao usuário digitar um ID de contato.

  • Usa esse valor de ID do cliente para executar um modelo a fim de recuperar informações de contato na tabela Person.Contact.

  • Exibe essas informações usando um formulário HTML.

O usuário pode atualizar informações de contato, mas não a ID de contato (porque ContactID é a chave primária). Depois que o usuário envia as informações, um diagrama de atualização é executado e todos os parâmetros do formulário são passados para o diagrama de atualização.

O seguinte modelo é o primeiro (GetContact.xml). Salve esse modelo no diretório associado ao nome virtual do tipo 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>  

O seguinte modelo é o segundo (UpdateContact.xml). Salve esse modelo no diretório associado ao nome virtual do tipo 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>  

O código a seguir é o aplicativo ASP (SampleASP.asp). Salve-o no diretório associado a uma raiz virtual criada usando o utilitário Gerenciador de Serviços de Internet. (Essa raiz virtual não é criada usando o Utilitário de Gerenciamento de Diretório Virtual do IIS para SQL Server porque o Gerenciamento de Diretório Virtual do IIS para SQL Server não pode acessar nem identificar aplicativos ASP.).

Observação

No código, você deve substituir "ServerName" pelo nome do servidor em que o IIS (Serviços de Informações da Internet da Microsoft) está em execução.

<% 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>  

Consulte Também

Considerações sobre segurança para diagramas de atualização (SQLXML 4.0)