我记得好象有一个网友问过关于怎样在几个站点间共享数据库资源
我在两台电脑上试验成功了我的代码是这样的提供大家参考
在站点a的数据库服务器的数据库中有一个数据表NoteBoard
包含字段ID(编号)Title(标题)NoterName(留言人名字)NoteTime(留言时间)
怎样可以让站点b获得这个数据表的记录呢
在a定义访问a站数据库的webservice文件MyViewDBServiceasmx
<%@WebService Language=C# Class=ViewDBService%>
using System;
using SystemData;
using SystemDataOleDb;
using SystemWebServices;
public class ViewDBService : WebService
{
[WebMethod]
public DataSet ViewDB()
{
string connStr=Provider=MicrosoftJetOLEDB;Data Source=E:\\WmjDBmdb;
OleDbConnection conn=new OleDbConnection(connStr);
string sqls=select IDTitleNoterNameNoteTime from NoteBoard order by id;
OleDbDataAdapter adapter=new OleDbDataAdapter();
adapterSelectCommand=new OleDbCommand(sqlsconn);
DataSet dataSet=new DataSet();
adapterFill(dataSetNoteBoard);
connClose();
return dataSet;
}
}
///////////////////////////////////////////////////////////////////////////////
假设这个webservice在
则作为客护端在站点b可以使用
wsdl /l:cs /n:DBService /out:ViewDBServiceClientcs
生成客户端文件 ViewDBServiceClientcs
用 csc /t:library /out:ViewDBServiceClientdll ViewDBServiceClientcs 编译dll
编写客户端网页文件indexaspx
<%@page language=C# Codebehind=indexaspxcs AutoEventWireup=false Inherits=WmjViewDB%>
<html>
<head>
<title>我的留言板</title>
</head>
<body>
<form runat=server>
<center>
<asp:DataGrid id=dataGrid ItemStyleBackColor=#AAAADD AutoGenerateColumns=false
AlternatingItemStyleBackColor=#CCCCFF HeaderStyleBackColor=#
HeaderStyleHorizontalAlign=Center
HeaderStyleForeColor=#FFFFFF PagerStyleMode=NumericPages
AllowPaging=true PageSize= FontSize=pt runat=server>
<columns>
<asp:BoundColumn HeaderText=序号 DataField=ID/>
<asp:BoundColumn HeaderText=标题 DataField=Title/>
<asp:BoundColumn HeaderText=留言人 DataField=NoterName/>
<asp:BoundColumn HeaderText=留言时间 DataField=NoteTime DataFormatString={:dd/MM/yyyy}/>
</columns>
</asp:DataGrid>
<asp:Label id=label runat=server/>
</center>
</form>
</body>
</html>
编写客户端文件的codebehind indexaspxcs
////////////////////////////////////////////////////////////////////
using System;
using SystemWebUI;
using SystemWebUIWebControls;
using SystemData;
using SystemDataOleDb;
using DBService; //引入客户端文件的名字空间
namespace Wmj
{
public class ViewDB : Page
{
protected DataGrid dataGrid;
public ViewDB()
{
thisInit+=new EventHandler(thisPage_Init);
}
public void Page_Init(object senderEventArgs e)
{
thisLoad+=new EventHandler(thisPage_Load);
thisdataGridPageIndexChanged+=new
DataGridPageChangedEventHandler(thisDataGrid_PageIndexChanged);
}
public void Page_Load(object senderEventArgs e)
{
ViewDBService viewDBService=new ViewDBService();
//使用webservice
dataGridDataSource=viewDBServiceViewDB()Tables[NoteBoard]DefaultView;
if(!PageIsPostBack)
{
dataGridCurrentPageIndex=;
dataGridDataBind();
}
}
public void DataGrid_PageIndexChanged(object senderDataGridPageChangedEventArgs e)
{
dataGridCurrentPageIndex=eNewPageIndex;
dataGridDataBind();
}
}
}