TechRepublic

The .NET Framework simplifies the Web service creation process. Learn how to create a Web service that interfaces with SQL Server and returns the applicable information from the Northwind database.

Overview
The System.Web.Services namespace provides the necessary classes for creating custom Web services. Specifically, a Web service is derived from the WebServices class located within this namespace. In addition, a Web service class file is created with the asmx file extension.

Web service methods are exposed via the WebMethod attribute. It immediately precedes the method name. The method marked with this attribute must be declared as public, so it is available to all as a Web service method should be.

You may use the .NET language of your choice, including C#, VB.NET, J#, or so forth. I will utilise both C# and VB.NET in this example. The remaining aspects of the development follow the normal rules of development.

Let's create our Web service to interface with SQL Server and return the applicable information from the Northwind database. There will be one Web service with six methods corresponding to the six stored procedures:

  • GetProductsById: Accepts an integer parameter. This parameter is used to call the sp_GetProductByID stored procedure.
  • GetProductsByName: Accepts a string parameter that is passed to the sp_GetProductByName stored procedure.
  • GetProductsByCategoryId: Accepts an integer parameter that is passed to the sp_GetProductByCategoryID stored procedure.
  • GetProductsByCategoryName: Accepts a string parameter that is passed to the sp_GetProductByCategoryName stored procedure.
  • GetProductsBySupplierId: Accepts an integer parameter that is passed to the sp_GetProductBySupplierID stored procedure.
  • GetProductsBySupplierName: Accepts a string parameter that is passed to the sp_GetProductBySupplierName stored procedure.

Each method is public and tagged with the WebMethod attribute. In addition, the SoapDocumentMethod attribute is assigned to each method as well to control SOAP formattnig.

For overall SOAP body formatting, or style, the Web Services Description Language (WSDL) provides two choices: RPC and document. The .NET Framework controls these choices in code using attributes. Document refers to formatting the XML Web service method according to an XSD schema. The document style refers to formatting the Body element as a series of one or more message parts following the Body element.

Listing A contains the code for the Web service I created.

Listing A

The Web service is simple, and this simplicity is aided by the Data Access Application Block (DAAB). DAAB is an excellent addition to a .NET developer's toolbox. It provides a clean and elegant method for access data in your .NET applications.

A great aspect of DAAB is the simple approach to creating methods. You can use it to return almost any data-related object including DataSet in our example. The overloaded method signatures ensure ease-of-use. They accept the same set of parameters: connection string, command type, SQL, and parameter object. The parameter object is only necessary when using stored procedures with parameters as this sample illustrates. I love interfacing with SQL Server in so few lines of code. Listing B contains the Web service written with VB.NET.

Listing B

Do you need help with .Net? Gain advice from Builder AU forums

Related links

Leave a comment

You must read and type the 6 chars within 0..9 and A..F

* indicates mandatory fields.

Log in


Sign up | Forgot your password?

  • Chris Duckett IE9's H.264 vote killed Ogg

    In a split decision by the judges, the winner of the W3C/WHATWG video codec consensus is H.264, taking home the future of video playback on the internet while loser Ogg goes home with nothing but thoughts of what might have been. Read more »

    -- posted by Chris Duckett

  • Staff Google launches Apps Marketplace

    Google launches and app store, while Mozilla plans to re-write its open-source license. More of this week's news in the Roundup. Read more »

    -- posted by Staff

  • Staff Microsoft showcases new NUIs

    TechFest, Microsoft's internal even took place this week with researchers showcasing some new interfaces the company is working on. Read more »

    -- posted by Staff

What's on?

  • Optus Deal

    Broadband + home phone + PlayStation®3 in a single package price!