Content by Category
.NET 1.x
.NET 2.0
.NET 3.0
.NET 3.5
.NET 4.0
.NET 4.5
.NET Assemblies
.NET Framework
.NET Getting Started
Accessibility
ADO.NET
Advertorials
Agile Development
AJAX
Amazon Web Services
Analysis Services
Android
Architecture
Arduino
ASP .NET Web API
ASP.NET
ASP.NET MVC
ASP.NET WebForms
Azure
B2B (Business Integration)
BDD
Big Data
Bing
BizTalk
Book Excerpts
Build and Deploy
Business Intelligence
C#
C++
ClickOnce
Cloud Computing
Code Contracts
CODE Framework Info - non Technical
CODE on the Road!
COM+
Community
Conferences
Continuous Integration
Crystal Reports
CSLA.NET
CSS
Data
Debugger
Design Patterns
Development Process
Display Technologies
Distributed Computing
Document Database
DotNetNuke
DSL
Dynamic Languages
Dynamic Programming
Editorials
Enterprise Services ("COM+")
Entity Framework
Events
Expression Blend
F#
Fox to Fox
Frameworks
Functional Programming
Git
Graphics
HTML 5
Internet Explorer 8.0
Interviews
IOS
iPhone
Iron Ruby
Java
Java Script
JavaScript
jQuery
JSON
Lightswitch
LINQ
Linux
LUA
Mac OS X
MDX
Messaging
Metro
Microsoft Application Blocks
Microsoft Business Rules Framework
Microsoft Dynamics
Microsoft Expression
Microsoft Office
Mobile Development
Mobile PC
Mono
MsBuild
MVVM
MySQL
Network
NHibernate
node.js
NOSQL
Nuget
Object Oriented Development
Objective C
Odata
OLAP
Open Source
Opinion
Opinions
Oracle
ORM
Other Languages
Parallel Programming
Patterns
PHP
Podcasts
Post Mortem
PowerPoint
Print/Output
Prism
Product News
Product Reviews
Project Management
Prolog
Python
Q&A
Rails
Rake
Razor
Reporting Services
REST
RIA Services
Ruby
Ruby on Rails
Scheme
Search
Security
Services
SharePoint
SignalR
Silverlight
SOA
Social Networks
Software & Law
Software Business
Source Control
Speech-Enabled Applications
SQL Server
SQL Server 2000
SQL Server 2005
SQL Server 2008
SQL Server 2012
SQL Server CE/AnyWhere/Mobile/Compact
SSIS
Subversion
Sync Framework
Tablet PC
TDD
Team System
Techniques
Testing and Quality Control
TFS
Tips
TypeScript
UI Design
UML
User Groups
VB Script
VB.NET
Version Control
VFP and .NET
VFP and SQL Server
Virtual Earth
Vista
Visual Basic
Visual Basic 6 (and older)
Visual FoxPro
Visual Studio .NET
Visual Studio 11
Visual Studio 2005
Visual Studio 2008
Visual Studio 2010
Visual Studio 2011
Visual Studio 2012
Visual Studio Tools for Office
VSX
WCF
Web Development (general)
Web Services
WebMatrix
WF
Whitepapers
Windows 7
Windows 8
Windows Azure
Windows Live
Windows Phone 7
Windows Phone SDK
Windows Server
Windows Vista
WinForms
WinRT
Workflow
WPF
XAML
Xiine Documentation
XML
XNA
XSLT



LearnNow


XAMALOT
 


SSWUG

Reader rating:
Click here to read 1 comment about this article.
Article source: CoDe (2002 - Jan/Feb)


Article Pages:  1  2 3 - Next >


XML UpdateGrams in SQL Server

The new XML features in SQL Server 2000 give the developer more power to implement distributed solutions. One of the newest features, XML UpdateGrams, allows the developer to handle the inserting, updating and deleting of records while getting around some of the limitations of URL queries and OPENXML. XML UpdateGrams perform their operations against an XML view, which is provided by an annotated XDR schema that contains the necessary information to map elements and attributes back to their corresponding tables and fields.

Until recently, developers have had to rely on OPENXML and stored procedures to modify data based on an XML document by parsing the document and providing it as a rowset that can then be manipulated like any other cursor or table. OPENXML, however, was clumsy to use, at best, as SQL Server does not provide an easy interface to iterate over a rowset record-by-record using Transact-SQL.

Rather than having to code multiple stored procedures to handle inserting, updating and deleting records within a database, you can use XML UpdateGrams instead. XML UpdateGrams, which are XML documents themselves, are posted to SQL Server's virtual directory and are intercepted by the SQL ISAPI extension. SQL Server can modify the data in the database by using various elements within an XML UpdateGram.

The basic premise behind an XML UpdateGram is that it behaves like a template, in that it uses a snapshot of before and after information to determine how SQL Server should proceed. By using a combination of <sync> elements, which mark the beginning and end of a transaction, and <before> and <after> elements, SQL Server can determine whether a new record is being created or an existing record is being modified or deleted.

The <before> element identifies the existing state of the database, while the <after> element identifies the database's new state. The <sync> element indicates what a transaction encompasses. It contains one or more <before> and <after> elements, and all the pairs within a <sync> element are executed as one transaction. So, either everything or nothing will be performed within a <sync> element.

The basic structure of an XML UpdateGram appears as follows:

<ROOT xmlns:updg="urn:schemas-microsoft-com:xml-updategram">
  <updg:sync [mapping-schema= "XDRSchemaFile.xml"]>
    <updg:before>
       ...
    </updg:before>
    <updg:after>
       ...
    </updg:after>
  </updg:sync>
</ROOT>

Note the inclusion of the UpdateGram namespace, urn:schemas-microsoft-com:xml-updategram. This namespace is required for XML UpdateGrams. However, the actual namespace you use within your UpdateGrams is completely up to you. For instance, the following is also a valid XML UpdateGram structure:

<ROOT xmlns:eps="urn:schemas-microsoft-com:xml-updategram">
  <eps:sync mapping-schema=["XDRSchemaFile.xml"]>
    <eps:before>
       ...
    </eps:before>
    <eps:after>
       ...
    </eps:after>
  </eps:sync>
</ROOT>

An XML UpdateGram determines what function to perform based upon the contents of the <before> and <after> elements, which is outlined as follows:

If a record's XML definition appears only in the <before> element, with no corresponding definition in the <after> element, the XML UpdateGram performs a delete operation.

If a record's XML appears only in the <after> element, with no corresponding XML in the <before> element, it is an insert operation.

If XML for a record appears in the <before> element and has a corresponding definition in the <after> element, it is an update operation. In this case, the UpdateGram updates the record instance to the values specified in the <after> element.

Mapping the XML to the Database

An XML UpdateGram can map the XML back into the database either implicitly or explicitly. In other words, specifying the XDR schema in the UpdateGram is optional, as long as you follow a few simple rules. First and foremost, you must use FOR XML AUTO or FOR XML AUTO,ELEMENTS in order to use the default mapping that SQL ISAPI provides.

In simple insert, update, or delete scenarios, implicit mapping may be enough to perform the necessary operation on the given XML fragment. SQL ISAPI attempts to map the elements and attributes back to the database in a similar fashion to the way it maps the database to an XML fragment using FOR XML AUTO. The key for this approach, however, is that each element, which represents a table, must be named the same as the table name. Remember, XML is case sensitive. Also, each element or attribute that represents a field must be named the same. For instance, the following XML UpdateGram will insert a new customer into the Customers table of the Northwind database:

<ROOT xmlns:updg="urn:schemas-microsoft-com:xml-updategram">
   <updg:sync >
      <updg:before>
      </updg:before>
      <updg:after>
         <Customers CustomerID="TEST" CompanyName="Test insert comp."/>
      </updg:after>
   </updg:sync>
</ROOT>

By examining the above XML UpdateGram, you can see that it identifies how SQL Server should behave. SQL ISAPI sees that there is nothing in the <before> element that corresponding to an <after> element; therefore an INSERT must be performed. SQL ISAPI also examines the contents of the <after> element and determines from the existence of the <Customers> element that a new record will be inserted into the Customers table. Furthermore, SQL ISAPI realizes that two fields will have values specified for them, CustomerID and CompanyName, based on the occurrence of attributes with those names.

For complex mappings, you can explicitly identify an annotated XDR schema to map the elements and attributes back to the appropriate tables and fields in the database. Each transaction unit, identified by a <sync> element, can have its own mapping schema to map elements and attributes back into the database.

&

By: Travis Vandersypen

Travis Vandersypen is a Senior Software Developer at EPS Software Corporation, located in Houston, Texas.

He concentrates on consulting on and developing COM-based, distributed applications. He is an international author and speaker, co-author of the Fox Extension Classes, co-author of the Voodoo Web Controls, and 3-time Visual FoxPro Excellence Award Nominee.

He can be reached via email at: travis@eps-software.com.

travis@dilmad.com

Fast Facts

XML UpdateGrams allow developers to INSERT, UPDATE and DELETE data from a SQL Server database without using OPENXML. XML UpdateGrams perform the desired operations against an XML view, which can be specified either implicitly or explicitly.



Article Pages:  1  2 3 - Next Page: 'Implicit Mapping in UpdateGrams' >>

Page 1: XML UpdateGrams in SQL Server
Page 2: Implicit Mapping in UpdateGrams
Page 3: Explicit Mapping in UpdateGrams (con't)

How would you rate the quality of this article?
1 2 3 4 5
Poor      Outstanding

Tell us why you rated the content this way. (optional)

Average rating:
2.5 out of 5

2 people have rated this article.

Instantly Search Terabytes Of Text
“Lightning Fast”
– Redmond Mag
“Covers all data
sources” – eWeek
25+ fielded & full-text search options
dtSearch’s own document filters highlight hits in popular file types
Web Spider supports static & dynamic data
APIs for .NET, Java, C++, SQL, etc.
Win / Linux (64-bit & 32-bit)
www.dtSearch.com
 

      Sharepoint TechCon

 

SSWUG