`

利用微软WebService技术实现远程数据库存取 利用web服务在不同站点间共享同一数据库

 
阅读更多
<iframe align="center" marginwidth="0" marginheight="0" src="http://www.zealware.com/csdnblog336280.html" frameborder="0" width="336" scrolling="no" height="280"></iframe>

利用微软WebService技术实现远程数据库存取


李凌宇
01-9-7 下午 01:18:14


随着微软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("");
for(int i=0;i
{
str.Append(" ");
");
str.Append("

号 码

str.Append(" ");
");
str.Append("选号费  
}
");
str.Append("
while(d.Read())
{
str.Append(" ");
str.Append(d["code"].ToString());
str.Append(" ");
str.Append(d["cost"].ToString());
");
str.Append("
if((d["status"].ToString().TrimEnd())=="已预定")
{
str.Append(" ");
str.Append("");
");
str.Append("
}
else
{
str.Append(" ");
str.Append("");
");
str.Append("
}
j++;
if(j%3==0)
{
");
str.Append("
}
}
d.Close();
con.Close();
str.Append("

");
if(pagenow==1)
{
str.Append("首页上一页");
}
else
{
str.Append("首页");
str.Append("上一页");
}
if(pagenow==page)
{
str.Append("下一页 尾页");
}
else
{
str.Append("下一页 尾页");
}
str.Append("页次:");
str.Append(pagenow);
str.Append("/");
str.Append(page);
str.Append("页");
str.Append("共");
str.Append(recordcount);
str.Append("个号码 39个号码/页");
return str.ToString();
}
private SqlDataReader GetCode(int pagenow,int cityid,out int recordcount)
{
SqlDataReader dr=null;
con=new SqlConnection("server=localhost;database=yitong;uid=sa;pwd=");
SqlCommand cmd=new SqlCommand("fenye",con);
cmd.CommandType=CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@pagenow",SqlDbType.Int));
cmd.Parameters["@pagenow"].Value=pagenow;//目前所在页面
cmd.Parameters.Add(new SqlParameter("@pagesize",SqlDbType.Int));
cmd.Parameters["@pagesize"].Value=39;//每页要显示的号码数
cmd.Parameters.Add(new SqlParameter("@cityid",SqlDbType.Int));
cmd.Parameters["@cityid"].Value=cityid;//城市代码
cmd.Parameters.Add(new SqlParameter("@code",SqlDbType.Char,3));
cmd.Parameters["@code"].Value="130";//只搜索联通的手机号码
SqlParameter q;
q=cmd.Parameters.Add(new SqlParameter("@recordcount",SqlDbType.Int));
q.Direction=ParameterDirection.Output;
con.Open();
cmd.ExecuteNonQuery();
recordcount=(int)cmd.Parameters["@recordcount"].Value;//返回的号码总数
dr=cmd.ExecuteReader();
return dr;
}
}
}
3. 客户端页面存放在B网站上。当客户浏览该网站时,通过该页面可以浏览、订购A网站数据库中号码。客户端页面使用微软的Webservice Behavior技术调用A上的web服务。WebService Behavior是微软在IE5.0中新增加的一项可以通过页面脚本使用web服务的技术。她使用SOAP协议与web服务通讯,可以动态更新页面的局部,而不刷新整个页面,较之通常使用的整页刷新的方法,它更快也更有效。要使用这项技术,须从微软网站上下载一个webservice.htc组件到客户端页面所在的目录下。
客户端页面的代码如下:
Client.htm
<script language="”javascript”"></script>
<!--</TD></TR></TBODY></TABLE><TABLE width="100%"><TBODY><TR><TD class=a14>function window_onload() {</TD></TR></TBODY></TABLE><TABLE width="100%"><TBODY><TR><TD class=a14>//调用A提供的web服务</TD></TR></TBODY></TABLE><TABLE width="100%"><TBODY><TR><TD class=a14>service.useService("http://IPofA/service1.asmx?WSDL","myselect");</TD></TR></TBODY></TABLE><TABLE width="100%"><TBODY><TR><TD class=a14>//调用web服务的table方法,显示第一个页面</TD></TR></TBODY></TABLE><TABLE width="100%"><TBODY><TR><TD class=a14>service.myselect.callService(showCode,"table",1,city.value);</TD></TR></TBODY></TABLE><TABLE width="100%"><TBODY><TR><TD class=a14>}</TD></TR></TBODY></TABLE><TABLE width="100%"><TBODY><TR><TD class=a14></TD></TR></TBODY></TABLE><TABLE width="100%"><TBODY><TR><TD class=a14>function city_onchange() {</TD></TR></TBODY></TABLE><TABLE width="100%"><TBODY><TR><TD class=a14>service.service1.callService(showCode,"table",1,city.value);</TD></TR></TBODY></TABLE><TABLE width="100%"><TBODY><TR><TD class=a14>}</TD></TR></TBODY></TABLE><TABLE width="100%"><TBODY><TR><TD class=a14></TD></TR></TBODY></TABLE><TABLE width="100%"><TBODY><TR><TD class=a14>function addcart(id)</TD></TR></TBODY></TABLE><TABLE width="100%"><TBODY><TR><TD class=a14>{ </TD></TR></TBODY></TABLE><TABLE width="100%"><TBODY><TR><TD class=a14>url = "basket.asp?code=" + id ;</TD></TR></TBODY></TABLE><TABLE width="100%"><TBODY><TR><TD class=a14>window.navigate(url);</TD></TR></TBODY></TABLE><TABLE width="100%"><TBODY><TR><TD class=a14>}</TD></TR></TBODY></TABLE><TABLE width="100%"><TBODY><TR><TD class=a14></TD></TR></TBODY></TABLE><TABLE width="100%"><TBODY><TR><TD class=a14>function next(x)</TD></TR></TBODY></TABLE><TABLE width="100%"><TBODY><TR><TD class=a14>{</TD></TR></TBODY></TABLE><TABLE width="100%"><TBODY><TR><TD class=a14>//显示下一页</TD></TR></TBODY></TABLE><TABLE width="100%"><TBODY><TR><TD class=a14>service.myselect.callService(showCode,"table",x,city.value)</TD></TR></TBODY></TABLE><TABLE width="100%"><TBODY><TR><TD class=a14>}</TD></TR></TBODY></TABLE><TABLE width="100%"><TBODY><TR><TD class=a14></TD></TR></TBODY></TABLE><TABLE width="100%"><TBODY><TR><TD class=a14>function first()</TD></TR></TBODY></TABLE><TABLE width="100%"><TBODY><TR><TD class=a14>{</TD></TR></TBODY></TABLE><TABLE width="100%"><TBODY><TR><TD class=a14>//显示首页</TD></TR></TBODY></TABLE><TABLE width="100%"><TBODY><TR><TD class=a14>service.myselect.callService(showCode,"table",1,city.value);</TD></TR></TBODY></TABLE><TABLE width="100%"><TBODY><TR><TD class=a14>}</TD></TR></TBODY></TABLE><TABLE width="100%"><TBODY><TR><TD class=a14></TD></TR></TBODY></TABLE><TABLE width="100%"><TBODY><TR><TD class=a14>function previous(x)</TD></TR></TBODY></TABLE><TABLE width="100%"><TBODY><TR><TD class=a14>{</TD></TR></TBODY></TABLE><TABLE width="100%"><TBODY><TR><TD class=a14>//显示上一页</TD></TR></TBODY></TABLE><TABLE width="100%"><TBODY><TR><TD class=a14>service.myselect.callService(showCode,"table",x,city.value);</TD></TR></TBODY></TABLE><TABLE width="100%"><TBODY><TR><TD class=a14>}</TD></TR></TBODY></TABLE><TABLE width="100%"><TBODY><TR><TD class=a14></TD></TR></TBODY></TABLE><TABLE width="100%"><TBODY><TR><TD class=a14>function last(x)</TD></TR></TBODY></TABLE><TABLE width="100%"><TBODY><TR><TD class=a14>{</TD></TR></TBODY></TABLE><TABLE width="100%"><TBODY><TR><TD class=a14>//显示最后一页</TD></TR></TBODY></TABLE><TABLE width="100%"><TBODY><TR><TD class=a14>service.myselect.callService(showCode,"table",x,city.value);</TD></TR></TBODY></TABLE><TABLE width="100%"><TBODY><TR><TD class=a14>}</TD></TR></TBODY></TABLE><TABLE width="100%"><TBODY><TR><TD class=a14></TD></TR></TBODY></TABLE><TABLE width="100%"><TBODY><TR><TD class=a14>function showCode(result)</TD></TR></TBODY></TABLE><TABLE width="100%"><TBODY><TR><TD class=a14>{</TD></TR></TBODY></TABLE><TABLE width="100%"><TBODY><TR><TD class=a14>//result保存调用web服务后返回的结果</TD></TR></TBODY></TABLE><TABLE width="100%"><TBODY><TR><TD class=a14>service.innerHTML=result.value;</TD></TR></TBODY></TABLE><TABLE width="100%"><TBODY><TR><TD class=a14>}</TD></TR></TBODY></TABLE><TABLE width="100%"><TBODY><TR><TD class=a14></TD></TR></TBODY></TABLE><TABLE width="100%"><TBODY><TR><TD class=a14>//-->
山东济南
山东济宁 山东东营
  可以看到,webservice behavior可以使一个静态页面通过脚本程序使用web服务,而且不用在客户端建立代理,只要拷贝一个webservice.htc组件就可以了。
  利用Visual Studio.Net,你可以不必了解HTTP、XML、SOAP、WSDL等底层协议,同样能开发和使用Web服务,真得是好爽。
附图:(这是我在本机调试时显示的页面,供参考)



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics