如何创建一个WebService

今天我们来看一下如何在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,在弹出的对话框中,
b) 我们选择"
Web services in this solution ",
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代码如下:
ContractedBlock.gif ExpandedBlockStart.gif Code
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;
        }
    }
}

运行下程序,网页上便会以分页形式把Customers表的数据全部以GridView列出来了。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值