How to Build a Web Service That Returns Data

■ To build a Web service that returns data, create a new Web service by using Visual Studio. NET. Typically, this Web Service will define one or more methods that:

• Establish a connection to a data source.

• Create a DataSet, define the structure of the resulting Typed DataSet (by using an .xsd file).

• Create an empty, local instance of the Strongly Typed DataSet.

• Run a query or perform calculations, and fill the local DataSet. A DataAdapter is commonly used to fill the dataset.

• Return the DataSet to the client application for further processing

Visual Basic Example

*****************************ILLEGAL for non"trainer use******************************

Introduction

Building a Web service that returns data

Creating a Web service is similar to creating any component that provides programmatic access to its application logic. To create a Web service, you need some functionality that constitutes the service you wish to expose, a service description that defines how to use the service, and an infrastructure to support receiving and processing requests, and sending responses. Fortunately, much of the required infrastructure is generated automatically by Visual Studio .NET.

To build a Web service that returns data, first create a new Web service by using Visual Studio. NET. Typically, this Web Service will define one or more methods that:

■ Establish a connection to a data source.

■ Create a DataSet, define the structure of the resulting Typed DataSet (by using an .xsd file).

■ Create an empty, local instance of the Strongly Typed DataSet.

■ Run a query or perform calculations, and fill the local DataSet. A DataAdapter is commonly used to fill the DataSet.

■ Return the DataSet to the client application for further processing.

Example The following example defines a Web method that takes a customer's city as input, queries the customers table in the Northwind database, and returns a DataSet with information about all the customers in that city.

This example assumes that a connection to the Northwind database exists.

'Connect to the Northwind DataBase Dim myCn as new SqlConnection myCn.ConnectionString = "data source=1oca1host;initia1 cata1og=Northwind" & _ "integrated security=SSPI;persist" & _ "security info=fa1se"

myCn.open

This example assumes that a SqlDataAdapter has been defined with the following parameterized query:

SQLDataAdapterl.SelectCommand.CommandText = _

SELECT Customerld, CompanyName, ContactName, Address, City,

Region, PostalCode, Country, Phone, Fax

FROM Customers

WHERE (City like @city)

Note When you create parameterized queries using the SQLDataAdapter, use named arguments to mark parameters.

When you create parameterized queries using the OLEDBDataAdapter, use the "?" character to mark parameters

'Set a query parameter for an OLDB data source

OLEDBDataAdapterl.SelectCommand.CommandText = _

SELECT Customerld, CompanyName, ContactName, Address, City,

Region, PostalCode, Country, Phone, Fax

FROM Customers

'Example of a Web Service that returns a DataSet Imports System.Web.Services Public Class Servicel

Inhe ri ts System.Web.Se rvi ces.WebSe rvi ce

'This method accepts a city name as a query parameter <WebMethod()> Public Function GetCustomers(ByVa1 city as String) as CustDS

'Create an instance of a Typed DataSet to hold the information 'retrieved from SQL Server Dim ds as New CustDS()

'Set the city parameter of the query, 0 is the first in 'the collection

SqlDataAdapter1.SelectCommand.Parameters(0).Value = city

'Fill the local DataSet with the results SqlDataAdapter1.fill(ds)

'Pass the results to the calling program Return ds

End Function

End Class

Practice A salesperson for Northwind Traders travels to various cities in order to visit customers and take orders. Since customer information changes frequently, the Customer Information application uses an XML Web service to retrieve information from the company's central database about customers in a particular city. The salesperson uses the application to generate the list of customers to visit while in a particular city.

► To create a Web service

1. Start Visual Studio.net and create a new project. Use the information in the following table.

Option Value

Project Type Visual Basic or Visual C#

Template ASP.Net Web service

Name CustomerInfo Service

2. Use the Server Explorer to add a new connection to the Northwind database on your local SQL Server.

3. Double Click on Service1.asmx in the Solution Explorer. This displays the design surface for the Web service.

4. Click the Data tab of the toolbox. Drag a SqlDataAdapter to the design surface. Use the information in the following table to configure the SqlDataAdapter by using the wizard.

Option Value

Connection Localhost.Northwind.dbo

Query type Use SQL statement

Query SELECT CustomerId, CompanyName,

ContactName, Address, City, Region, PostalCode, Country, Phone, Fax

FROM Customers

WHERE (City like @city)

5. Right click on SQLDataAdapter1 and then click Generate DataSet. Use the information in the following table.

Property Value

New DataSet Name CustDS

Tables Customer

Add DataSet to the designer Checked

6. Right click CustDS.xsd and then click View Schema. Examine the generated schema.

► To create a Web method that returns a dataset

1. View the code for Servicel.asmx.

2. Create a new Web method by inserting the following code after the commented example.

<WebMethod()> Public Function GetCustomers(ByVa1 city as _ String) as CustDS

Dim ds as New CustDS()

SqlDataAdapterl.SelectCommand.Parameters(O).Value = city SqlDataAdapterl.fill(ds) Return ds End Function

3. Build the project.

► To test the Web service

1. Right click Servicel.asmx and then click Browse With...Choose Microsoft Internet Explorer from the list.

2. Right click Servicel.asmx and then click View in Browser.

3. Examine the default page generated to describe the Web service.

4. Click GetCustomers.

5. Test the Web method by using "London" as the parameter value. Click Invoke.

6. Examine the XML returned by the Web service. Notice that it contains both schema and data.

0 0

Post a comment