DATABASE

SQL Server 2005 Native XML Web Services : Example Native XML Web Services Project (part 1) - Creating the SQL Server Functionality

3/23/2013 4:07:09 AM
Having examined the basics of exposing Native XML Web Services, let’s look at a sample application. For our example, we will use the fictitious Adventure Works Cycles, a small bicycle company that is the basis for the samples that ship with SQL Server 2005. In our scenario, Adventure Works Cycles has just hired a new sales manager who is responsible for increasing revenue. He has decided that the sales representatives need mobile access to customer information while they are on the road visiting client stores. He has named this application Adventure Sales and has written up a list of what the application must allow sales representatives to do:
  1. Search for client stores by name or location (to help minimize travel time by scheduling visits to multiple stores in a locality).

  2. View address and contact information for stores (to facilitate making appointments and getting directions to the stores).

  3. View the status of orders recently placed by the store (to be able to field questions about orders during a visit).

Our detail-oriented sales manager ordered smartphone PDAs for the sales force and announced his new initiative to the senior executives and the sales team before consulting with the IT manager.

The IT department at Adventure Works Cycles is small and extremely busy. The only person available to work on the project is a junior developer who does not have the time or expertise to develop a full-blown n-tier system. However, after analyzing the requirements, the savvy IT manager realizes that because the company’s data is stored on SQL Server 2005, they can complete this project in a few days using a simple Windows Forms application to run under Windows Mobile 5.0 and to access the data via Native XML Web Services.

Note

Although our scenario demands a Windows Mobile solution, the sample code is implemented as a standard C# Windows Forms project. This allows you to open the project and work with the code without needing additional setup to support Windows Mobile 5.0. Because Windows Mobile 5.0 supports invoking Web services, this application can quite easily be rewritten as a mobile application.


1. Creating the SQL Server Functionality

We’ll be creating three new stored procedures and a UDF to implement the required SQL Server functionality for our application. The SQL scripts for creating these are in the SQL Server Management Studio sample project . You can open this project and run the scripts in the order they are discussed in this section. This way, you’ll be ready to expose them as Web methods in the Native XML Web Services endpoint we will be creating later in the section.

To demonstrate returning data as XML and consuming it on the client side, the first stored procedure, GetStoreInfo, shown in Listing 1, takes a store CustomerID and returns the store name and address as XML.

Listing 1. GetStoreInfo from GetStoreInfo.sql
USE AdventureWorks;
GO
IF OBJECT_ID ( 'dbo.GetStoreInfo', 'P' ) IS NOT NULL
    DROP PROCEDURE [dbo].[GetStoreInfo];
GO
CREATE PROCEDURE [dbo].[GetStoreInfo]
   @CustomerID [int]
AS
BEGIN

SELECT Store.Name, Address.AddressLine1, Address.City, Address.PostalCode,
   StateProvince.StateProvinceCode as State
FROM   Sales.Store
   INNER JOIN Sales.CustomerAddress
   ON Store.CustomerID = CustomerAddress.CustomerID
   INNER JOIN Person.Address
   ON CustomerAddress.AddressID = Address.AddressID
   INNER JOIN Person.StateProvince
   ON Address.StateProvinceID = StateProvince.StateProvinceID
   AND Address.StateProvinceID = StateProvince.StateProvinceID
WHEREStore.CustomerID=@CustomerID
FOR XML AUTO, ELEMENTS

END;
GO

					  

The other two stored procedures simply return the results of an SQL query without using the FOR XML functionality. We will use the DEFAULT choice for the WSDL option when we create the endpoint so that the data will be returned to the client application as a DataSet.

Note

We can safely return a DataSet in our example because we know the client application is using the .NET Framework 2.0. If you need to support non-Microsoft clients, you should use the simple WSDL when setting up the client proxy to ensure compatibility, and you should avoid .NET-specific types.


The GetStoreContacts stored procedure shown in Listing 2 returns all of the contacts associated with the store identified by the passed CustomerID.

Listing 2. GetStoreContacts from GetStoreContacts.sql
USE AdventureWorks;
GO
IF OBJECT_ID ( 'dbo.GetStoreContacts', 'P' ) IS NOT NULL
    DROP PROCEDURE [dbo].[GetStoreContacts];
GO
CREATE PROCEDURE [dbo].[GetStoreContacts]
   @CustomerID [int]
AS
BEGIN

   SET NOCOUNT ON;

   SELECT  Contact.FirstName, Contact.LastName,
               Contact.EmailAddress, Contact.Phone
   FROM    Person.Contact
               INNER JOIN Sales.StoreContact
               ON Contact.ContactID = StoreContact.ContactID
               INNER JOIN Sales.Store
               ON StoreContact.CustomerID = Store.CustomerID
   WHERE     (Store.CustomerID = @CustomerID)

END;
GO

The GetStoreRecentOrders stored procedure is shown in Listing 3. It returns orders that are less than two months old or are still open.

Listing 3. GetStoreRecentOrders from GetStoreRecentOrders.sql
USE AdventureWorks;
GO
IF OBJECT_ID ( 'dbo.GetStoreRecentOrders', 'P' ) IS NOT NULL
    DROP PROCEDURE [dbo].[GetStoreRecentOrders];
GO
CREATE PROCEDURE [dbo].[GetStoreRecentOrders]
   @CustomerID [int]
AS
BEGIN

DECLARE @TwoMonthsAgo DATETIME

SELECT @TwoMonthsAgo = DATEADD(Month, -26, GETDATE())
SELECT      SalesOrderHeader.OrderDate, SalesOrderHeader.DueDate, SalesOrderHeader
             .ShipDate, dbo.ufnGetSalesOrderStatusText(Sales.SalesOrderHeader.Status)
               AS OrderStatus
FROM        Sales.Store
               INNER JOIN Sales.SalesOrderHeader
                  ON Sales.Store.CustomerID = Sales.SalesOrderHeader.CustomerID
WHERE((Sales.SalesOrderHeader.OrderDate>@TwoMonthsAgo
                 OR Sales.SalesOrderHeader.DueDate>@TwoMonthsAgo)
   OR (Sales.SalesOrderHeader.Status<>5 AND Sales.SalesOrderHeader.Status<>6)
)
               AND (Sales.Store.CustomerID = @CustomerID)
ORDER BY Sales.Store.CustomerID, OrderDate DESC
END;
GO

					  

Note

Because all the sample data in the AdventureWorks database is dated before 2005, we cheated a bit in the GetStoreRecentOrders stored procedure in Listing 10-4 and subtracted 26 months from the current date instead of 2 months, as the requirements for the scenario demand. Depending on when you run the example, you might need to tweak this value or add new data to the AdventureWorks database to get recent orders to show up in the application.


The ufnGetStoreSalesYTD user-defined function is shown in Listing 4. It returns the sum of the sales orders placed by the store in the current year.

Listing 4. ufnGetStoreSalesYTD from ufnGetStoreSalesYTD.sql
USE AdventureWorks;
GO
IF OBJECT_ID ( 'dbo.ufnGetStoreSalesYTD', 'FN' ) IS NOT NULL
    DROP FUNCTION [dbo].[ufnGetStoreSalesYTD];
GO
CREATE FUNCTION [dbo].[ufnGetStoreSalesYTD](@CustomerID Int)
RETURNS [decimal]
AS
BEGIN
   DECLARE @ThisYear Int
   SELECT @ThisYear = DATEPART(year, GETDATE())-2
   DECLARE @YTDSale decimal

   SELECT @YTDSale = SUM(SubTotal)
   FROM Sales.SalesOrderHeader
   WHERE CustomerID=@CustomerID AND DATEPART(year, OrderDate)=@ThisYear

RETURN @YTDSale
END;
GO

Note

As with the GetStoreRecentOrders stored procedure, we had to cheat a bit in the ufnGetStoreSalesYTD user-defined function shown in Listing 10-5 to get non-zero values. We did this by subtracting two years from the current year to get back to where AdventureWorks data exists. Depending on when you run the example, you might need to tweak this value or add new data to the AdventureWorks database to get recent orders to show up in the application.

Other  
  •  SQL Server 2005 Native XML Web Services : Exposing SQL Programmability as Web Services (part 2) - Calling Native XML Web Service Endpoints from Client Applications
  •  SQL Server 2005 Native XML Web Services : Exposing SQL Programmability as Web Services (part 1)
  •  Western Digital Black 4TB Hard Drive - 4TB Storage Goes Mainstream
  •  Intel Solid State Drive 335 Series - Better Under The Hood
  •  HDD, SSD and Hybrid Hard Drive Competition
  •  SQL Server 2008 : Index analysis (part 3) - Identifying index fragmentation
  •  SQL Server 2008 : Index analysis (part 2) - Identifying indexes to add
  •  SQL Server 2008 : Index analysis (part 1) - Identifying indexes to drop/disable
  •  ADO.NET Programming : Microsoft SQL Server CE (part 5) - Querying Schema Information
  •  ADO.NET Programming : Microsoft SQL Server CE (part 4) - Updating a SQL Server CE Database, The SqlCeDataAdapter Class
  •  
    Top 10
    3 Tips for Maintaining Your Cell Phone Battery (part 2) - Discharge Smart, Use Smart
    3 Tips for Maintaining Your Cell Phone Battery (part 1) - Charge Smart
    OPEL MERIVA : Making a grand entrance
    FORD MONDEO 2.0 ECOBOOST : Modern Mondeo
    BMW 650i COUPE : Sexy retooling of BMW's 6-series
    BMW 120d; M135i - Finely tuned
    PHP Tutorials : Storing Images in MySQL with PHP (part 2) - Creating the HTML, Inserting the Image into MySQL
    PHP Tutorials : Storing Images in MySQL with PHP (part 1) - Why store binary files in MySQL using PHP?
    Java Tutorials : Nested For Loop (part 2) - Program to create a Two-Dimensional Array
    Java Tutorials : Nested For Loop (part 1)
    REVIEW
    - First look: Apple Watch

    - 3 Tips for Maintaining Your Cell Phone Battery (part 1)

    - 3 Tips for Maintaining Your Cell Phone Battery (part 2)
    VIDEO TUTORIAL
    - How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 1)

    - How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 2)

    - How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 3)
    Popular Tags
    Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8 BlackBerry Android Ipad Iphone iOS