zoukankan      html  css  js  c++  java
  • 利用Remoting远程插入数据

    Step1:建立服务器程序:Server.cs
    Step2:建立业务组件:RemoteServer.cs
    Step3:客户端调用程序:ClientWin.cs

    服务器端界面
      

    客户端界面
     

    代码:
    Server.cs
    private void Form1_Load(object sender, System.EventArgs e)
      {
       
       filename = "SrvProflile.ini";
       string path = @"\mydir\";
       fullpath = Path.GetFullPath(filename);
       try
       {
        TcpChannel channel = new TcpChannel(Convert.ToInt16(RIniFile(fullpath,"Port")));//此处是读取ini文件来得到用户指定的端口 此处测试用8058 
        ChannelServices.RegisterChannel(channel);
        dotNetBarManager1.GetItem("ServerStatus").Text="服务端已经启动,请单击<开始服务>按钮开始服务....";
       }
       catch(Exception ex)
       {
        MessageBox.Show(ex.Message.ToString());
        this.Close();
       }

      }

    private void StartService_Click(object sender, System.EventArgs e)
      {

       try
       {
        service1 = new ServerObject();
        ObjRef objRef = RemotingServices.Marshal((MarshalByRefObject)service1,"service1");
        dotNetBarManager1.GetItem("ServerStatus").Text="对象已经启动....";
       }
       catch(Exception ex)
       {
        MessageBox.Show(ex.Message);
       }

      }

    private string RIniFile(string fp,string val)
      {
       StringBuilder temp = new StringBuilder ( 255 ) ;
       string FileName = fp ;
       string section = "Server" ;
       string key = val ;
       int i = GetPrivateProfileString ( section , key ,"无法读取对应数值!" , temp , 255 , FileName ) ;
       //显示读取的数值
       return temp.ToString();

      }


    RemoteServer.cs
    namespace ServerRemoteObject
    {
     /// <summary>
     /// Class1 的摘要说明。
     /// </summary>
     ///

     //Cargo业务区
     [Serializable]
     public class CargoInfo
     {
      public CargoInfo()
      {
      }
      private string cmpname;
      private string tel;
      private string fax;
      private string address;
      private string name;
      private string mobile;
      private string bigbillno;

      public string Cmpname
      {
       get {return cmpname;}
       set {cmpname = value;}
      }

      public string Tel
      {
       get {return tel;}
       set {tel = value;}
      }

      public string Fax
      {
       get {return fax;}
       set {fax = value;}
      }

      public string Address
      {
       get {return address;}
       set {address = value;}
      }

      public string Name
      {
       get {return name;}
       set {name = value;}
      }

      public string Mobile
      {
       get {return mobile;}
       set {mobile = value;}
      }

      public string BigBillNo
      {
       get {return bigbillno;}
       set {bigbillno = value;}
      }
       
     }
     

     public class ServerObject:MarshalByRefObject
     {


      //读ini文件
      [ DllImport ( "kernel32" ) ]
      private static extern int GetPrivateProfileString ( string section ,
       string key , string def , StringBuilder retVal ,
       int size , string filePath ) ;


      //Read SerProFile.ini
      private static string RIniFile(string val)
      {
       string filename = "SrvProflile.ini";
       string fullpath;
       fullpath = Path.GetFullPath(filename);

       StringBuilder temp = new StringBuilder ( 255 ) ;
       string FileName = fullpath ;
       string section = "Server" ;
       string key = val ;
       int i = GetPrivateProfileString ( section , key ,"无法读取对应数值!" , temp , 255 , FileName ) ;
       //显示读取的数值
       return temp.ToString();

      }

      public string name;
      public int j = 2;
      

      SqlConnection conn = new SqlConnection("server="+"RIniFile("SrvIP")+"+";uid="+RIniFile("UID")+";pwd="+RIniFile("PWD")+";database=MH_DB");


      public string tst(CargoInfo cin)
      {
       return cin.Cmpname;//DateTime.Now.ToString();
      }

      public ServerObject()
      {
       //
       // TODO: 在此处添加构造函数逻辑
       //
      }
      
      public DataSet GetAuthors()
      {
       DataSet ds = new DataSet(); 
       conn.Open();
       SqlDataAdapter ada = new SqlDataAdapter("select * from authors",conn);
       conn.Close();
       ada.Fill(ds,"a");
       return ds;

      }

      public string InsertCargo(DataSet ds,CargoInfo CI)
       {
        SqlCommand sqlIncomm;
        int i;
        SqlDataAdapter sqlDataAdapter2 = new System.Data.SqlClient.SqlDataAdapter();
        sqlIncomm = new System.Data.SqlClient.SqlCommand();
        sqlIncomm.CommandText = "INSERT INTO Cargo_Table(C_BIGBILLNO) VALUES (@C_BIGBILLNO)";
        sqlIncomm.Parameters.Add(new System.Data.SqlClient.SqlParameter("@C_BIGBILLNO", System.Data.SqlDbType.VarChar, 50, "C_BIGBILLNO"));

        sqlIncomm.Connection = this.conn;
        sqlDataAdapter2.InsertCommand = sqlIncomm;
       
        try
        {
         conn.Open();
         for(i=0;i<ds.Tables[0].Rows.Count;i++)
         {    
          sqlIncomm.Parameters["@C_BIGBILLNO"].Value =CI.BigBillNo
          sqlDataAdapter2.InsertCommand.ExecuteNonQuery();
         }
         conn.Close();
         return "保存数据成功";
        }
        catch(Exception ex)
        {
         return ex.Message.ToString();
        }   
      }  
     }
    }
    ClientWin.cs
    private void frmMain_Load(object sender, System.EventArgs e)
      {
       tabStrip1.MdiForm=this;

       try
       {
        TcpChannel tcpChannel = new TcpChannel();
        ChannelServices.RegisterChannel(tcpChannel);
       }
       catch(Exception ex)
       {
        MessageBox.Show(ex.Message.ToString());
       }
      }

    private void bt_Save_Click(object sender, System.EventArgs e)
      {
       try
       {
        CargoInfo ci = new CargoInfo();
        ServerObject obj;
        obj = (ServerObject)Activator.GetObject(typeof(ServerObject),"tcp://192.168.0.103:8058/service1");
        ci.Cmpname=this.tb_out_com.Text;
        ci.Tel = this.tb_out_tel.Text;
        ci.Fax = this.tb_out_fax.Text;
        ci.Address = this.tb_out_address.Text;
        ci.Name = this.tb_out_man.Text;
        ci.Mobile = this.tb_out_mobile.Text;
        ci.BigBillNo = this.big_no.Text;
        //保存数据
        int ds_record;
        ds_record = this._CargoDS.Tables[0].Rows.Count;
        if(ds_record>0)
        {
         string total = obj.InsertCargo(this._CargoDS,ci);
          MessageBox.Show(total);
        }
        else
        {
         MessageBox.Show("没有可以保存的数据","Error");
          return;
        }

       }
       catch(Exception ex)
       {
           MessageBox.Show(ex.Message.ToString(),"Error");
       }
      }

    不当之处请指教!

  • 相关阅读:
    OSG-提示“error reading file e:1.jpg file not handled”
    OSG-加载地球文件报0x00000005错误,提示error reading file simple.earth file not handled
    QT-找开工程后,最上方提示the code model could not parse an included file, which might lead to incorrect code completion and highlighting, for example.
    我的书《Unity3D动作游戏开发实战》出版了
    java中无符号类型的第三方库jOOU
    Windows批处理备份mysql数据
    使用 DevTools 时,通用Mapper经常会出现 class x.x.A cannot be cast to x.x.A
    Java版本,Java版本MongoDB驱动,驱动与MongoDB数据库,Spring之间的兼容性
    Jrebel本地激活方法
    wget下载指定网站目录下的所有内容
  • 原文地址:https://www.cnblogs.com/Jery/p/135691.html
Copyright © 2011-2022 走看看