intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

Creating a Web Service

Chia sẻ: Qweqwdasd Qweqdasda | Ngày: | Loại File: PDF | Số trang:5

152
lượt xem
47
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Tạo ra một dịch vụ Web Trong phần này, bạn sẽ tạo ra một dịch vụ Web có chứa một phương thức trả về một DataSet có chứa các hàng từ bảng Khách hàng. Bắt đầu VS NET và. Chọn File ➣ New ➣ dự án. Trong hộp thoại New Project, chọn Visual C # Dự án trong khung dự án Các loại trái, và chọn ASP.NET Web Service trong cửa sổ Templates ở bên phải. Nhập http://localhost/NorthwindWebService trong lĩnh vực Địa điểm (xem hình 17,1). Click OK để tiếp tục. ...

Chủ đề:
Lưu

Nội dung Text: Creating a Web Service

  1. 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. 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.
  2. 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 using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Web; using System.Web.Services; namespace NorthwindWebService { /// /// Summary description for Customers. /// /// [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;
  3. /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { } /// /// Clean up any resources being used. /// 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"; // } } 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
  4. 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 [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; } 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;
  5. 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.
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
6=>0