今天我們來看一下如何在VS2008中建立并應用一個基本的WebService。
場景:利用VS2008建立一個WebService,改服務取得Northwind下的 Customers表格資料。
ASPX頁面調用該服務,并将結果以GridView的形式顯示在界面上。
首先,我們當然是做一個service了。
我們通過菜單生成一個Web Service,命名為:Customers,它的完整名字是:Customers.asmx。
代碼如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace BlogNet.WebService
{
[WebService(Namespace = "http://www.cnblogs.com/davidgu/customers")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Customers : System.Web.Services.WebService
{
[WebMethod]
public DataSet GetCustomers()
{
SqlConnection conn;
SqlDataAdapter myDataAdapter;
DataSet myDataSet;
string cmdString = "Select * From Customers";
conn = new SqlConnection("Data Source=localhost;Initial Catalog=Northwind;Integrated Security=True");
myDataAdapter = new SqlDataAdapter(cmdString, conn);
myDataSet = new DataSet();
myDataAdapter.Fill(myDataSet, "Customers");
return myDataSet;
}
}
}
然後,我們需要在我們的solution中引用該服務。
滑鼠點選solution,
a) 右鍵菜單->Add Web Reference,在彈出的對話框中,
c) 再選擇我們剛剛建立的Customers服務。
d) 然後在Web reference Name中我們給我們的服務取個有意義的名字為:WS_Customers。
這些做好以後,我們可以觀察在Web.config檔案中自動生成了一些東西,如下:
<applicationSettings>
<BlogNet.Properties.Settings>
<setting name="BlogNet_WS_Customers_Customers" serializeAs="String">
<value>http://localhost:1408/WebService/Customers.asmx</value>
</setting>
</BlogNet.Properties.Settings>
</applicationSettings>
</configuration>
最後,我們寫一下用戶端頁面如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="testCustomer.aspx.cs" Inherits="BlogNet.WebService.testCustomer" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ASP.NET - 建立WebService</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1"
runat="server"
AutoGenerateColumns="False"
AllowPaging="True"
AllowSorting="True"
PageSize="20"
OnPageIndexChanging="GridView1_PageIndexChanging">
<Columns>
<asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True"
SortExpression="CustomerID" NullDisplayText="N/A" />
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName"
SortExpression="CompanyName" NullDisplayText="N/A" />
<asp:BoundField DataField="ContactName" HeaderText="ContactName"
SortExpression="ContactName" NullDisplayText="N/A" />
<asp:BoundField DataField="ContactTitle" HeaderText="ContactTitle"
SortExpression="ContactTitle" NullDisplayText="N/A" />
<asp:BoundField DataField="Address" HeaderText="Address"
SortExpression="Address" NullDisplayText="N/A" />
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" NullDisplayText="N/A" />
<asp:BoundField DataField="Region" HeaderText="Region"
SortExpression="Region" NullDisplayText="N/A" />
<asp:BoundField DataField="PostalCode" HeaderText="PostalCode"
SortExpression="PostalCode" NullDisplayText="N/A" />
<asp:BoundField DataField="Country" HeaderText="Country"
SortExpression="Country" NullDisplayText="N/A" />
<asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" NullDisplayText="N/A" />
<asp:BoundField DataField="Fax" HeaderText="Fax" SortExpression="Fax" NullDisplayText="N/A" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
CS代碼如下:
Code
運作下程式,網頁上便會以分頁形式把Customers表的資料全部以GridView列出來了。