Search
Database

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

Published 5 min readBy Programming4us Editorial Team
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


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

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


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

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

Note


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

Note