日期:2014-05-18  浏览次数:21020 次

winform/remoting。如何使用客户端操纵服务器端内存中的数据
程序使用winform形式,分别放在不同的机器,一个服务器端,多个客户端。服务器端启动时会将数据读到内存,客户端要使用服务器端读到内存中的数据。请问:如何在客户端也能改变服务器端中内存中的数据?

使用remoting....

先谢了。

------解决方案--------------------
--RemotingClass:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
namespace RemotingClass
{
public class FarClass 
{
public static int i;
SqlConnection conn;
public FarClass()
{
conn = new SqlConnection();
conn.ConnectionString = "Data Source=localhost;Database=test;User ID=sa;Password=";
}

public DataTable GetDataTable(string TableName)
{
SqlDataAdapter sda = new SqlDataAdapter("select * from " + TableName, conn);
DataSet ds = new DataSet();
sda.Fill(ds,TableName);
return ds.Tables[TableName];
}

}
}
remoting Server:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Services;
using System.Runtime.Remoting.Channels.Tcp;
using RemotingClass;
namespace RemotingServer
{
public partial class RemotingServerForm : Form
{
public RemotingServerForm()
{
InitializeComponent();
}

private void RemotingServerForm_Load(object sender, EventArgs e)
{
TcpChannel chan = new TcpChannel(9999);
ChannelServices.RegisterChannel(chan);
FarClass fc = new FarClass();
ObjRef obj = RemotingServices.Marshal(fc, "Tcpservice");
RemotingServices.Unmarshal(obj);
//RemotingConfiguration.Configure("RemotingServer.exe.Config", false);
this.label1.Text = "服务端已启动";
}
}
}
Remoting Client:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Reflection;
using System.Diagnostics;
using RemotingClass;
using System.IO;
namespace RemotingClient
{
public partial class RemotingClientForm : Form
{
FarClass fc;
DataTable table;
public RemotingClientForm()
{
InitializeComponent();
}

private void RemotingClientForm_Load(object sender, EventArgs e)
{
ChannelServices.RegisterChannel(new TcpChannel());
WellKnownClientTypeEntry RemotingConfing = new WellKnownClientTypeEntry(typeof(FarClass), "tcp:localhost:9999/TcpService");
RemotingConfiguration.RegisterWellKnownClientType(RemotingConfing);
//RemotingConfiguration.Configure("RemotingClient.exe.Config",false);
fc = new FarClass();
}

private void button4_Click(object sender, EventArgs e)
{
table = new DataTable();
table = fc.GetDataTable("student");
this.dataGridView1.DataS