Creating a Web Service

In this section, you'll create a Web service that contains a method that returns a DataSet containing rows from the Customers table.

Start VS .NET and select File New Project. In the New Project dialog box, select Visual C# Projects in the Project Types pane on the left, and select ASP.NET Web Service in the Templates pane on the right. Enter http://localhost/NorthwindWebService in the Location field (see Figure 17.1). Click OK to continue.

click to expand
Figure 17.1: Creating a Web service in VS .NET

Note 

If you have installed IIS on a computer other than your local machine, then replace localhost with the name of your remote computer in the Location field.

After VS .NET has created the new project, open Solution Explorer and delete the Service1.asmx file from your project; you'll be adding your own .asmx file next, and it's easier to simply delete the initial Service1.asmx file.

Select Project Add Web Service, and enter Customers.asmx in the Name field of the Add New Item dialog box (see Figure 17.2). Click Open to continue. VS .NET adds a file named Customers.asmx to your project.

click to expand
Figure 17.2: Adding a new Web service

Select View Code to view the C# code in the Customers.asmx.cs file. Listing 17.1 shows my example Customers.asmx.cs file.

Listing 17.1: CUSTOMERS.ASMX.CS

start example
 using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Web; using System.Web.Services; namespace NorthwindWebService {   /// <summary>   /// Summary description for Customers.   /// </summary>   ///   [WebService(Namespace="http://DbProgramming/NorthwindWebService")]   public class Customers : System.Web.Services.WebService   {     public Customers()     {       //CODEGEN: This call is required by the ASP.NET Web Services Designer       InitializeComponent();     }     #region Component Designer generated code     //Required by the Web Services Designer     private IContainer components = null;     /// <summary>     /// Required method for Designer support - do not modify     /// the contents of this method with the code editor.     /// </summary>     private void InitializeComponent()     {     }     /// <summary>     /// Clean up any resources being used.     /// </summary>     protected override void Dispose(bool disposing)     {       if(disposing && components != null)       {         components.Dispose();       }       base.Dispose(disposing);     }     #endregion     // WEB SERVICE EXAMPLE     // The HelloWorld() example service returns the string Hello World     // To build, uncomment the following lines then save and build the project     // To test this web service, press F5 //  [WebMethod] //  public string HelloWorld() //  { //    return "Hello World"; //  } } 
end example

Notice that the Customers class is derived from the System.Web.Services.WebService class, which indicates that the Customers class forms part of a Web service.

Near the end of Listing 1.1, you'll notice a method named HelloWorld() that is commented out. This commented code shows you how to write a method that is exposed by your Web service. You'll notice that a line containing [WebMethod] is placed before the method, which indicates that the method would be exposed by the Web service. Of course, because the HelloWorld() method is commented out, the method isn't compiled and therefore isn't actually exposed by the Web service.

Replace the example HelloWorld() method in your code with the RetrieveCustomers() method shown in Listing 17.2. RetrieveCustomers() connects to the Northwind database and returns a DataSet containing rows from the Customers table. You pass a WHERE clause to the RetrieveCustomers() method in the whereClause parameter; this WHERE clause is then used in the SELECT statement to limit the rows retrieved from the Customers table.

Listing 17.2: CUSTOMERSWEBSERVICE.CS

start example
 [WebMethod] public DataSet RetrieveCustomers(string whereClause) {   SqlConnection mySqlConnection =     new SqlConnection("server=localhost;database=Northwind;uid=sa;pwd=sa");   string selectString =     "SELECT CustomerID, CompanyName, Country "+     "FROM Customers "+     "WHERE "+ whereClause;   SqlCommand mySqlCommand = mySqlConnection.CreateCommand();   mySqlCommand.CommandText = selectString;   SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();   mySqlDataAdapter.SelectCommand = mySqlCommand;   DataSet myDataSet = new DataSet();   mySqlConnection.Open();   mySqlDataAdapter.Fill(myDataSet, "Customers");   mySqlConnection.Close();   return myDataSet; } 
end example

Note 

You'll need to change the string used to create the mySqlConnection object in your code to connect to your Northwind database.

Because the code uses classes in the System.Data.SqlClient namespace, you'll also need to add the following line near the top of your Customers.asmx.cs file:

 using System.Data.SqlClient; 

By default, a Web service uses a namespace of http://tempuri.org, and you should change that to the URL used by your organization. The following example sets the namespace for the Web service to http://DbProgramming/NorthwindWebService:

 [WebService(Namespace="http://DbProgramming/NorthwindWebService")] public class Customers : System.Web.Services.WebService 

Notice that you set the Namespace in a line placed before the Customers class. Go ahead and add a line similar to the previous one to your own code.

Build your Web service by selecting Build Build Solution.

That's it! You've built your Web service.




Mastering C# Database Programming
Mastering the SAP Business Information Warehouse: Leveraging the Business Intelligence Capabilities of SAP NetWeaver
ISBN: 0764596373
EAN: 2147483647
Year: 2003
Pages: 181

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net