完整的网站间共享数据的WebService
在站点a的数据库服务器的数据库中有一个数据表NoteBoard
包含字段ID(编号),Title(标题),NoterName(留言人名字),NoteTime(留言时间)
怎样可以让站点b获得这个数据表的记录呢。
在a定义访问a站数据库的webservice文件MyViewDBService.asmx
<%@WebService Language="C#" Class="ViewDBService"%>
using System;
using System.Data;
using System.Data.OleDb;
using System.Web.Services;
public class ViewDBService : WebService
{
[WebMethod]
public DataSet ViewDB()
{
string connStr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\WmjDB.mdb";
OleDbConnection conn=new OleDbConnection(connStr);
string sqls="select ID,Title,NoterName,NoteTime from NoteBoard order by id";
OleDbDataAdapter adapter=new OleDbDataAdapter();
adapter.SelectCommand=new OleDbCommand(sqls,conn);
DataSet dataSet=new DataSet();
adapter.Fill(dataSet,"NoteBoard");
conn.Close();
return dataSet;
}
}
///////////////////////////////////////////////////////////////////////////////
假设这个webservice在http://www.a.com/MyViewDBService.asmx
则作为客护端在站点b可以使用
wsdl /l:cs /n:DBService /out:ViewDBServiceClient.cs http://www.w.com/MyViewDBService.asmx
生成客户端文件 ViewDBServiceClient.cs
用 csc /t:library /out:ViewDBServiceClient.dll ViewDBServiceClient.cs 编译dll
编写客户端网页文件index.aspx
<%@page language="C#" Codebehind="index.aspx.cs" AutoEventWireup="false" Inherits="Wmj.ViewDB"%>
<html>
<head>
<title>我的留言板</title>
</head>
<body>
<form runat="server">
<center>
<asp:DataGrid id="dataGrid1" ItemStyle-BackColor="#AAAADD" AutoGenerateColumns="false"
AlternatingItemStyle-BackColor="#CCCCFF" HeaderStyle-BackColor="#000000"
HeaderStyle-HorizontalAlign="Center"
HeaderStyle-ForeColor="#FFFFFF" PagerStyle-Mode="NumericPages"
AllowPaging="true" PageSize="4" Font-Size="10pt" runat="server">
<columns>
<asp:BoundColumn HeaderText="序号" DataField="ID"/>
<asp:BoundColumn HeaderText="标题" DataField="Title"/>
<asp:BoundColumn HeaderText="留言人" DataField="NoterName"/>
<asp:BoundColumn HeaderText="留言时间" DataField="NoteTime" DataFormatString="{0:dd/MM/yyyy}"/>
</columns>
</asp:DataGrid>
<asp:Label id="label1" runat="server"/>
</center>
</form>
</body>
</html>
编写客户端文件的codebehind index.aspx.cs
////////////////////////////////////////////////////////////////////
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
using DBService; //引入客户端文件的名字空间
namespace Wmj
{
public class ViewDB : Page
{
protected DataGrid dataGrid1;
public ViewDB()
{
this.Init+=new EventHandler(this.Page_Init);
}
public void Page_Init(object sender,EventArgs e)
{
this.Load+=new EventHandler(this.Page_Load);
this.dataGrid1.PageIndexChanged+=new
DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
}
public void Page_Load(object sender,EventArgs e)
{
ViewDBService viewDBService=new ViewDBService();
//使用webservice
dataGrid1.DataSource=viewDBService.ViewDB().Tables["NoteBoard"].DefaultView;
if(!Page.IsPostBack)
{
dataGrid1.CurrentPageIndex=0;
dataGrid1.DataBind();
}
}
public void DataGrid1_PageIndexChanged(object sender,DataGridPageChangedEventArgs e)
{
dataGrid1.CurrentPageIndex=e.NewPageIndex;
dataGrid1.DataBind();
}
}
}