Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
194 Chapter 10. Modifying Data with Updategrams I explained in previous chapters how you can retrieve data through a SQLISAPI virtual directory or by writing COM- or .NET-based code. However, retrieving data solves only half the problem. We also need a way to modify data by updating records, deleting records, or adding new records. For example, the Northwind Traders Web site might allow users not only to view the products available, but also to place orders by adding records to the Orders and Order Details tables. Or Northwind Traders could use an intranet-based application to update employee records. XML updategrams and DiffGrams are two similar technologies that give developers an XML-based approach to data modification, in much the same way that templates provide you with an XML-based way to retrieve data. In this chapter, I'll show you how to create and use updategrams to insert, delete, and update SQL Server data. I'll discuss DiffGrams in the next chapter. Note You can create a virtual directory to test the updategrams in this chapter by running the BuildAll.bat script in the Demos\Chapter10 folder in the book's sample files. This batch file uses a script called CreateVDir.vbs to create a SQLXML virtual directory. Once the virtual directory is created, you can access the updategrams described in this chapter by using a browser at http://localhost/chapter10/updategrams . Updategram functionality was added to Microsoft SQL Server 2000 in SQLXML 1.0. It provides a way for you to perform database update operations by creating XML documents that contain the necessary before and after images of the data being modified. The corresponding elements in the <after> block indicate how the record will look after the updategram completes the operation. The updategram uses the information in the <after> block to create the necessary Transact-SQL state- ment to make the required changes in the database. You can use an updategram in the same way that you use templates: They can be stored in a SQLISAPI virtual directory and executed over HTTP, or they can be submitted as commands using ADO or the SqlXml managed classes. Anatomy of an Updategram An updategram is based on the xml-updategram namespace and contains one or more sync ele- ments. Each sync element represents a transactional unit of database modifications. Sync elements contain at least one pair of before and after elements representing an individual insert, update, or delete operation. For example, the following updategram could be used to change the Home- Phone field in the Employees table for employee number 1: <?xml version="1.0"?> <EmployeeUpdate xmlns:updg="urn:schemas-microsoft-com:xml-updategram"> <updg:sync> <updg:before> <Employees EmployeeID="1"/> </updg:before> <updg:after>