NewsRhzhi | 先创资讯 | 旧版入口
rhzhi.net
网站首页 | NewsRhzhi | 先创资讯 | 操作系统 | 工具软件 | 办公软件 | 网站设计 | 组网专栏 | 平面设计 | 多 媒 体 | 程序开发 | 硬件资料 | 聊天软件
您现在的位置: 先创网 >> 程序开发 >> .NET >> 文章正文
完整的网站间共享数据的WebService(Love.NET原创)
不详
2005-3-14文/佚名
    

我记得好象有一个网友问过关于怎样在几个站点间共享数据库资源
我在两台电脑上试验成功了我的代码是这样的提供大家参考
在站点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();
    }
  }
}





打印此页 投稿与建议 返回顶部
栏 目 索 引
软件应用 SOFTWARE
Win XP | NT/2003
Win2000 | DOS/Win9x
PowerPoint | Office
Excel | Word
网络软件 | 实用软件
媒体软件 | 系统软件
常用软件 | 办公软件
聊天软件 | 网络安全
新软试用 | Vista
设计在线 DESIGN
Dreamweaver | 3DMax
Photoshop | Flash
平面设计 | 网页设计
多 媒 体 | 精品画廊
精彩专区 SPECIAL
Q Q 专区 | 热门专题
组网玩网 | 程序开发
应用集锦 |

没有任何图片文章
相关文章
关于我们 - 联系方式 - 合作伙伴 - 网站大事记 - 网站地图 - 我要投稿
Copyright ©1997-2008 先创网 All Rights Reserved.
先创科技 版权所有