服務器端遠程對象using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;namespace QQShare
{
  
    public interface IServerQQ
    {
        excuteSql getExcute();
    }      [System.Serializable]
    public class excuteSql
    {
     private string cnstr = " Persist Security Info = false;Server=192.168.1.254;uid=sa;Pwd=;database=northwind";
      
        private string getConnectionString()
        {
            return cnstr;
        }
        public SqlDataReader getReader(string sql)
        {
            try
            {
                SqlConnection cn = new SqlConnection(getConnectionString());
                cn.Open();
                SqlCommand cm = new SqlCommand(sql, cn);                return cm.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
服務器:
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.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Services;namespace QQServer
{
    public partial class frmServer : Form
    {
        public frmServer()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            TcpServerChannel sch = new TcpServerChannel(99);
            ChannelServices.RegisterChannel(sch, false);
            RemotingConfiguration.RegisterWellKnownServiceType(typeof(QQServer.Server), "Test", WellKnownObjectMode.SingleCall);
            button1.Text = "Start...";
        }    }
    public class Server : MarshalByRefObject,QQShare.IServerQQ
    {
       QQShare.excuteSql share = null;
       public QQShare.excuteSql getExcute()
        {
            if (share == null)
            {
                share = new QQShare.excuteSql();
            }
            return share;
        }
    }
}
客戶端遠程對象(只是把的連接字符串變成空)namespace QQShare
{
  
    public interface IServerQQ
    {
        excuteSql getExcute();
    }      [System.Serializable]
    public class excuteSql
    {
     private string cnstr = "";
      
        private string getConnectionString()
        {
            return cnstr;
        }
        public SqlDataReader getReader(string sql)
        {
            try
            {
                SqlConnection cn = new SqlConnection(getConnectionString());
                cn.Open();
                SqlCommand cm = new SqlCommand(sql, cn);                return cm.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
客戶端代碼:
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.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting;
using System.Data.SqlClient;namespace QQClient
{
    public partial class frmClient : Form
    {
        public frmClient()
        {
            InitializeComponent();
        }        private void frmClient_Load(object sender, EventArgs e)
        {
            init();
        }
        QQShare.IServerQQ qo;
        public void init()
        {
            TcpClientChannel channel = new TcpClientChannel();
            ChannelServices.RegisterChannel(channel, false);
            qo = (QQShare.IServerQQ)Activator.GetObject(typeof(QQShare.IServerQQ), "Tcp://192.168.1.18:99/Test");            
        }        private void button1_Click(object sender, EventArgs e)
        { 
            QQShare.excuteSql ee = qo.getExcute();    
            SqlDataReader dr = ee.getReader("select * from orders");
            dr.Read();
            this.textBox1.Text = dr[0].ToString();
        }       
    }
}
這是沒問題的。現在有一個奇怪的問題就是:如果把的連接字符串變成const或static(private const string cnstr=)客戶端執行getReader()方法時就提示ConnectionString是空的。什麼原因,如何解決?