日期:2012-09-22  浏览次数:20467 次

随着微软Visual Studo.Net Beta版的发布,由于Visual Studio.Net对XML以及Web服务的强大支持,利用Visual Studio.Net开发Web服务应用将会越来越多而且是非常的方便。本文以一个B2B电子商务网站为例,介绍利用web服务在不同站点间共享同一数据库的具体方法和步骤。本文中,客户端是指使用web服务的一方,服务器端是指提供web服务的另一方。

问题的提出

  该网站是一家(简称A)从事网上销售手机SIM卡的业务的电子商务网站。前不久,该网站与另一家网站(简称B)合作,共同开展网上销售联通手机SIM卡业务。由于都是使用的A网站的号码资源,存取的都是A网站的数据库,于是笔者利用webservice技术为另一家网站开发了网上售卡系统。

各主要功能的模块和关键代码

1. 数据库采用SQL SERVER2000,使用存储过程实现号码浏览的分页显示。代码如下:
create procedure fenye
(
@pagenow int,
@pagesize int,
@cityid int,
@code char(3),
@recordcount int output
)
as
set nocount on

declare @allid int,@beginid int,@endid int,@pagebegin char(11),@pageend char(11)

select @allid=count(*) from jinan where cityid=@cityid and (code like @code+'%')
select @recordcount=@allid

declare cur_fastread cursor scroll for
SELECT code FROM jinan where cityid=@cityid and (code like @code+'%') order by code

open cur_fastread
select @beginid=(@pagenow-1)*@pagesize+1
select @endid=@beginid+@pagesize-1

fetch absolute @beginid from cur_fastread into @pagebegin

if @endid>@allid
fetch last from cur_fastread into @pageend
else
fetch absolute @endid from cur_fastread into @pageend

set nocount off

select code,cost,status from jinan join xuanhaofei on jinan.category=xuanhaofei.category and jinan.cityid=xuanhaofei.cityid
where code between @pagebegin and @pageend order by code

close cur_fastread
deallocate cur_fastread

GO

2. 用Visual Studio.net创建webservice。在visual studo.net中,webservice文件的扩展名是.asmx。该文件放在A网站上,供其他网站调用。
* 启动visual studio.net,选择new project。
* 在左面版中选择visual c# projects,在右面版中选择ASP.NET WebService。
* 单击ok按钮就生成了一个webservice项目。在项目中新建一个webservice文件,WebService1.asmx。该文件实现对数据库的存取,并对调用者输出一个字符串。

下面是该文件的代码:

WebService1.asmx.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Text;
using System.Diagnostics;
using System.Web;
using System.Web.Services;

namespace WebService1
{
public class Service2 : System.Web.Services.WebService
{
SqlConnection con;

public Service2()
{
//CODEGEN: This call is required by the ASP.NET Web Services Designer
InitializeComponent();
}

[WebMethod] //[WebMethod]属性声明此方法作为web服务可以被远程使用者调用
public string table(int pagenow,int cityid)
{
int recordcount;//总号码数
int page=0;//总页数
int j=0;

SqlDataReader d=GetCode(pagenow,cityid,out recordcount);

if(recordcount%39==0)
{
page=recordcount/39;//每页只显示39个号码

}
else
{
page=recordcount/39+1;
}

StringBuilder str=new StringBuilder("<table border='1' width='100%' bordercolorlight='00008B' bordercolordark='#fffff0' cellspacing='0' cellpadding='0' height='24'><tr>");

for(int i=0;i<3;i++)
{
str.Append("<td bgcolor='#f0f8ff' align='middle' height='0' valign='center'>");
str.Append("<p style='MARGIN-BOTTOM: 2px'><font size='2'>号 码</font></p></td>");
str.Append("<td bgcolor='#f0f8ff' align='middle' height='0' valign='center'>");
str.Append("<font size='2'>选号费</font></td><td bgcolor='#f0f8ff' align='middle' height='0' valign='center'> </td>");
}

str.Append("</tr><tr>");

while(d.Read())
{
str.Append("<td height='24' align='middle'><font size='2'>");
str.Append(d["code"].ToString());
str.Append("</td><td height='24' align='middle'><font size=