利用微软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("
1<table border="1" bordercolordark="#fffff0" bordercolorlight="00008B" cellpadding="0" cellspacing="0" height="24" width="100%"><tr>");
2---
3---
4for(int i=0;i<3;i++)
5---
6{
7---
8str.Append("<td align="middle" bgcolor="#f0f8ff" height="0" valign="center">");
9---
10str.Append("<p style="MARGIN-BOTTOM: 2px"><font size="2">号 码</font></p></td>");
11---
12str.Append("<td align="middle" bgcolor="#f0f8ff" height="0" valign="center">");
13---
14str.Append("<font size="2">选号费</font></td><td align="middle" bgcolor="#f0f8ff" height="0" valign="center"> </td>");
15---
16}
17---
18---
19str.Append("</tr><tr>");
20---
21---
22while(d.Read())
23---
24{
25---
26str.Append("<td align="middle" height="24"><font size="2">");
27---
28str.Append(d["code"].ToString());
29---
30str.Append("</font></td><td align="middle" height="24"><font size="2"> ");
31---
32str.Append(d["cost"].ToString());
33---
34str.Append("</font></td>");
35---
36---
37if((d["status"].ToString().TrimEnd())=="已预定")
38---
39{
40---
41str.Append("<td align="middle" height="24">");
42---
43str.Append("<input name="image" src="images/hand.gif" type="image"/>");
44---
45str.Append("</td>");
46---
47}
48---
49else
50---
51{
52---
53str.Append("<td align="middle" height="24">");
54---
55str.Append("<input height="24" name="image" onclick='javascript:addcart(");
56---
57str.Append(d["code"].ToString());
58---
59str.Append(")' src="images/cart.jpg" type="image" width="24"/>");
60---
61str.Append("</td>");
62---
63}
64---
65---
66j++;
67---
68if(j%3==0)
69---
70{
71---
72str.Append("</tr><tr>");
73---
74}
75---
76---
77}
78---
79d.Close();
80---
81con.Close();
82---
83---
84str.Append("</tr></table>
1<br/>
");
if(pagenow==1)
{
str.Append("
1<font color="#000080" size="2">首页 上一页</font>
");
}
else
{
str.Append("
1<font color="#000080" size="2"><a href="javascript:first()">首页</a> ");
2---
3str.Append("<a href='javascript:previous(");
4---
5str.Append(pagenow-1);
6---
7str.Append(")'>上一页</a></font>
");
}
if(pagenow==page)
{
str.Append("
1<font color="#000080" size="2">下一页 尾页</font>
");
}
else
{
str.Append("
1<a href='javascript:next(");
2---
3str.Append(pagenow+1);
4---
5str.Append(")'><font color="#000080" size="2">下一页</font></a>
1<a href='javascript:last(");
2---
3str.Append(page);
4---
5str.Append(")'>尾页</a>
");
}
str.Append("
1<font color="#000080" size="2"> 页次:</font>
1<strong><font color="red" size="2">");
2---
3str.Append(pagenow);
4---
5str.Append("</font><font color="#000080" size="2">/");
6---
7str.Append(page);
8---
9str.Append("</font></strong>
页 ");
str.Append("
1<font color="#000080" size="2"> 共<b>");
2---
3str.Append(recordcount);
4---
5str.Append("</b>个号码 <b>39</b>个号码/页</font>
");
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
1<html>
2---
3<head>
4---
5<title></title>
6---
7<script language="”javascript”">
8---
9<!--
10---
11function window_onload() {
12---
13//调用A提供的web服务
14---
15service.useService("http://IPofA/service1.asmx?WSDL","myselect");
16---
17//调用web服务的table方法,显示第一个页面
18---
19service.myselect.callService(showCode,"table",1,city.value);
20---
21}
22---
23---
24function city_onchange() {
25---
26service.service1.callService(showCode,"table",1,city.value);
27---
28}
29---
30---
31function addcart(id)
32---
33{
34---
35url = "basket.asp?code=" + id ;
36---
37window.navigate(url);
38---
39}
40---
41---
42function next(x)
43---
44{
45---
46//显示下一页
47---
48service.myselect.callService(showCode,"table",x,city.value)
49---
50}
51---
52---
53function first()
54---
55{
56---
57//显示首页
58---
59service.myselect.callService(showCode,"table",1,city.value);
60---
61}
62---
63---
64function previous(x)
65---
66{
67---
68//显示上一页
69---
70service.myselect.callService(showCode,"table",x,city.value);
71---
72}
73---
74---
75function last(x)
76---
77{
78---
79//显示最后一页
80---
81service.myselect.callService(showCode,"table",x,city.value);
82---
83}
84---
85---
86function showCode(result)
87---
88{
89---
90//result保存调用web服务后返回的结果
91---
92service.innerHTML=result.value;
93---
94}
95---
96---
97//-->
98---
99</script>
100---
101</head>
102---
103<body onload="return window_onload()">
104---
105<select language="javascript" name="city" onchange="return city_onchange()">
106---
107<option selected="" value="531"> 山东济南</option>
108---
109<option value="537"> 山东济宁</option> <option value="546"> 山东东营</option>
110---
111</select>
112---
113<div id="service" style="behavior:url(webservice.htc)">
114---
115</div>
116---
117</body>
118---
119</html>