Usando um diagrama de atualização em um aplicativo ASP de exemplo (SQLXML 4.0)
Este aplicativo ASP (Active Server Pages) permite atualizar informações do cliente na tabela Person.Person do banco de dados de exemplo AdventureWorks2008R2 no Microsoft SQL Server. O aplicativo faz o seguinte:
Pede ao usuário para digitar a ID de uma entidade de negócios.
Usa esse valor de ID para executar um modelo para recuperar informações de contato na tabela Person.Person.
Exibe essas informações usando um formulário HTML.
O usuário pode atualizar as informações de contato, mas não a ID da entidade de negócios (porque a BusinessEntityID é 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 modelo a seguir é o primeiro (GetPerson.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="bid"></sql:param>
</sql:header>
<sql:query>
SELECT *
FROM Person.Person
WHERE BusinessEntityID=@bid
FOR XML AUTO
</sql:query>
</root>
O modelo a seguir é o segundo (UpdatePerson.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="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>
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 IIS Virtual Directory Management do utilitário SQL Server porque o IIS Virtual Directory Management do SQL Server não pode acessar ou 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 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>
Observação