Build a .NET Remoting program (real three-layer structure)

xiaoxiao2021-03-06  44

Build a .NET Remoting program (real three-layer structure)

Build a .NET Remoting program (real three-layer structure) _ 突 血 推 推!!! Net Remoting provides a powerful and efficient way to process remote objects. When I developed a power software, because the traffic is large, three layers must be used. Implement data processing (browsing, update), but this area is very small. By groping what I found. Only give the C # programmer who wants to use .NET development three-layer structure database application and bitter to find the method. Step 1: Create a shared library Click "File" -> "New Create" -> "Project", select Create a C # Library, and then name YDGLServerLibrary, then click the OK button. This will create a "shared command set" for our .NET Remote client and server. Front is complete code using System; using System.Runtime; using System.Data.OleDb; using System.Data.SqlClient; using System.Data; using System.Configuration; namespace ydglServerClassLibrary1 {///

/// Class1 Abstract illustration.

/// public class ydglDB: System.MarshalByRefObject {private System.Data.OleDb.OleDbDataAdapter oleDbDataAdapter1; private System.Data.OleDb.OleDbCommand oleDbSelectCommand1; private System.Data.OleDb.OleDbCommand oleDbInsertCommand1; private System.Data. OleDb.OleDbCommand oleDbUpdateCommand1; private System.Data.OleDb.OleDbCommand oleDbDeleteCommand1; private System.Data.OleDb.OleDbConnection oleDbConnection1; private System.ComponentModel.Container components = null; public void InitializeComponent () {this.oleDbDataAdapter1 = new System.Data. OleDb.OleDbDataAdapter (); this.oleDbSelectCommand1 = new System.Data.OleDb.OleDbCommand (); this.oleDbInsertCommand1 = new System.Data.OleDb.OleDbCommand (); this.oleDbUpdateCommand1 = new System.Data.OleDb.OleDbCommand () Im.oledbdeleteCommand1 = new system.data.oledb.oledbcommand (); this.Oledb Connection1 = new System.Data.OleDb.OleDbConnection (); // // oleDbDataAdapter1 // this.oleDbDataAdapter1.DeleteCommand = this.oleDbDeleteCommand1; this.oleDbDataAdapter1.InsertCommand = this.oleDbInsertCommand1; this.oleDbDataAdapter1.SelectCommand = this.oleDbSelectCommand1; THISOOLDBDATAADAPTER1.TABLEMAPPINGS.ADDRANGE (new system.data.common.dataablemapping [] {new system.data.common.DataTableMapping ("Table", "BM_GDFGS"

New system.data.common.datacolumnmapping [] {new system.data.common.datacolumnmapping ("code", "code"), new system.data.common.Datacolumnmapping ("name", "name")}} ); this.oleDbDataAdapter1.UpdateCommand = this.oleDbUpdateCommand1; // // oleDbSelectCommand1 // this.oleDbSelectCommand1.CommandText = "SELECT code, name FROM bm_gdfgs"; this.oleDbSelectCommand1.Connection = this.oleDbConnection1; // // oleDbInsertCommand1 / / This.oledbinsertcommand1.commandtext = "INSERT INTO BM_GDFGS (CODE, NAME) VALUES (?,?); Select code, name from bm_gdfgs w" "here (code =?)"; this.oleDbInsertCommand1.Connection = this.oleDbConnection1; this.oleDbInsertCommand1.Parameters.Add (new System.Data.OleDb.OleDbParameter ( "code", System.Data.OleDb.OleDbType.VarChar, 2, "code")); this .oleDbInsertCommand1.Parameters.Add (new System.Data.OleDb.OleDbParameter ( "name", System.Data.OleDb.OleDbType.VarChar, 40, "name")); this.oleDbInsertCommand1.Parameters.Add (new System.Data .Oledb.oledbparameter ("select_code", system.data.oledb.oledbtype.varchar, 2, "code"))))); // oledbupdateCommand1 // this.oledbupdateCommand1.comMandText = "

Update bm_gdfgs set code =?, Name =? Where (code =?) And (name =? Or? Is NUL " " l and name is null); SELECT CODE, NAME FROM BM_GDFGS WHERE (Code =?) "; THIS .oleDbUpdateCommand1.Connection = this.oleDbConnection1; this.oleDbUpdateCommand1.Parameters.Add (new System.Data.OleDb.OleDbParameter ( "code", System.Data.OleDb.OleDbType.VarChar, 2, "code")); this. oleDbUpdateCommand1.Parameters.Add (new System.Data.OleDb.OleDbParameter ( "name", System.Data.OleDb.OleDbType.VarChar, 40, "name")); this.oleDbUpdateCommand1.Parameters.Add (new System.Data. OLEDB.OLDBPARETER ("Original_code", System.Data.Oledb.oledbtype.varchar, 2, System.Data.ParameterDirection.Input, False, ((System.byte)) ((System.byte) (0) ), "code", System.Data.DataRowVersion.Original, null)); this.oleDbUpdateCommand1.Parameters.Add (new System.Data.OleDb.OleDbParameter ( "original_name", System.Data.OleDb.OleDbType.VarChar, 40 , System.Data.ParameterDirection.Input, False, (0)), ((System.byte) (0)), "Name", System.Data.DataRowVersion.original, null); this.OleDbupdateCommand1.Parameters.add (New System.Data. OLEDB.OLDBPARAMETER ("Original_name1", System.Data.Oledb.oledbtype.varchar, 40, System.Data.ParameterDirection.Input, False ((System.byte) ((System.byte) (0) ), "name", System.Data.DataRowVersion.Original, null)); this.oleDbUpdateCommand1.Parameters.Add (new System.Data.OleDb.OleDbParameter ( "Select_code", System.Data.OleDb.OleDbType.VarChar, 2 , "Code"));

// // oleDbDeleteCommand1 // this.oleDbDeleteCommand1.CommandText = "DELETE FROM bm_gdfgs WHERE (? Code =) AND (name = OR IS NULL AND name IS NULL??" ")"; This.oleDbDeleteCommand1.Connection = this. oleDbConnection1; this.oleDbDeleteCommand1.Parameters.Add (new System.Data.OleDb.OleDbParameter ( "Original_code", System.Data.OleDb.OleDbType.VarChar, 2, System.Data.ParameterDirection.Input, false, ((System.Byte (0)), ((System.byte) (0)), "CODE", System.Data.DataRowVersion.original, Null); this.oledbdeleteCommand1.Parameters.add (new system.data.oledb.oledbParameter) "Original_Name", System.Data.Oledb.oledbtype.varchar, 40, System.Data.ParameterDirection.Input, False, ((System.byte)), ((System.byte)), "Name ", System.Data.DataRowVersion.Original, null)); this.oleDbDeleteCommand1.Parameters.Add (new System.Data.OleDb.OleDbParameter (" Original_name1 ", System.Data.OleDb.OleDbType.VarChar, 40, System.Data .Parameterd IRECTION.INPUT, FALSE, ((System.byte) ((System.byte) (0)), "Name", System.Data.DataRowVersion.original, Null); // // OLEDBConnection1 / / this.oleDbConnection1.ConnectionString = @ "User ID = sa; Data Source = BILLGATES; Tag with column collation when possible = False; Initial Catalog = ydgl; Use Procedure for Prepare = 1; Auto Translate = True; Persist Security Info = False ,;,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

// // Service1 //} public DataSet Get_bm_gdfgs () {DataSet ds = new DataSet (); oleDbDataAdapter1.Fill (ds); return ds;} public DataSet Update_bm_gdfgs (DataSet ds) {if (! Ds = null) {oleDbDataAdapter1 .UPDATE (DS); Return DS;} else {return null;}}}}, please remember, if you get the system.runtime.remoting.channels.tcp namespace does not exist, please check if the code above is added. Quote for System.Runtime.Remoting.dll. Compiling and created projects will get a DLL file and can be used in other projects. Step 2: Creating a Server object There are several ways to create a Server object. The most intuitive method is the following method: in Visual Studio.net, click "File" -> "New Creation" -> "Project", select Create a "Windows Application" and name it YDGLServer. The most important thing is that we need to add applications that have been created in the first step, which can operate correctly. Click "Project" -> Add References in turn, then add a reference to the DLL file created in the first step by clicking the "Browse" button. To use the .NET Remote feature, you must add a reference to the DLL file by selecting "Project" -> Add Reference. Select System.Runtime.Remoting.dll in the .NET tab, and then click the "OK" button. Then, you need to add a reference to System.Runtime.Remoting.DLL as we in the first step. The following object is quite simple and intuitive, I will explain each line of the three-line code related to .NET Remoting. TCPServerChannel is one of the two channel types supported by .NET Remoting, which will set our object to respond to the request from which port, ChannelServices.RegisterChannel will put the port number and the TCP / IP in the operating system. Stack binding. TcpServerChannel channel = new TcpServerChannel (9932); ChannelServices.RegisterChannel (channel); a channel may be provided another type of HTTP, it is simply used HttpServerChannel System.Runtime.Remoting.Channels.Http namespace objects to get. The difference between the HTTP and TCP channels can be simply concatenated: if the application is running on the LAN, it is best to use the TCP channel because its performance is better than the HTTP channel; if the application is running on the Internet, then Sometimes the HTTP is the only choice according to the configuration of the firewall.

It is necessary to remember that if a firewall software is used, the firewall should configure the TCP data traffic through the port selected by you. RemotingConfiguration.regiSterwellknownServiceType (Type Foader "," ResumeLoader ", WellkNownObjectMode.singlecall); This line code sets some of the parameters in the service and binds the name of the object you want to use with the remote object. The first parameter is bound. Objects, the second parameter is a string of the remote object name in the TCP or HTTP channel, and the third parameter allows the container to know when there is a request to the object, how to handle the object. Although WellkNownObjectMode.single uses an instance of an object to all callars, it generates an instance of this object for each customer. Complete object code as follows: using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Runtime.Remoting; using System.Runtime. Remoting.channels; use system.runtime.remoting.channels.tcp; using ydglserClassLibrary1; Namespace Ydglserver1 {///

/// Form1 summary description. /// public class Form1: System.Windows.Forms.Form {private System.Windows.Forms.PictureBox pictureBox1; private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; private System.Windows.Forms.Button button3; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label3; private System.Windows.Forms.ComboBox comboBox1; / // /// The required designer variable. /// private system.componentmodel.container Components = NULL; Public Form1 () {// // Windows Form Designer Support To Support // InitializationComponent (); // // Todo: Call in InitializationComponent Add any constructor code //} /// /// Clean all the resources being used.

/// Protected Override Void Dispose (Bool Disposing) {if (disponents! = Null) {Components.dispose ();}} Base.Dispose (DISPOSION);} #Region Windows Form The code generated by the designer ///

/// designer supports the required method - do not use the code editor to modify the // / this method.

/// private void InitializeComponent () {System.Resources.ResourceManager resources = new System.Resources.ResourceManager (typeof (Form1)); this.pictureBox1 = new System.Windows.Forms.PictureBox (); this. Button1 = new system.windows.Forms.Button (); this.button2 = new system.windows.Forms.Button (); this.button3 = new system.windows.Forms.Button (); this.label1 = new system. Windows.Forms.Label (); this.label2 = new system.windows.forms.label (); this.label3 = new system.windows.forms.label (); this.comboBox1 = new system.windows.Forms.comBobox (); This.suspendlayout (); // // PictureBox1 // this.picturebox1.image = ((System.drawing.Image) ("picturebox1.image))); this.picturebox1.location = New system.drawing.point (32, 96); this.picturebox1.name = "picturebox1"; this.picturebox1.size = new system.drawi Ng.size (56, 48); this.picturebox1.tabindex = 0; this.picturebox1.tabstop = false; // // button1 // this.button1.drawing.image) (resources.drawing.image) (resources.drawing.image) ("Button1.image"))); this.button1.location = new system.drawing.point (253, 52); this.button1.name = "button1"; this.button1.size = new system.drawing.size (32, 32); this.button1.tabindex = 1; // // button2 // this.button2.image = ((System.drawing.Image) ("Button2.image)));

This.Button2.Location = new system.drawing.point (253, 100); this.button2.name = "button2"; this.button2.size = new system.drawing.size (32, 32); this.button2. TabINDEX = 2; // // Button3 // this.Button3.Image = ((System.drawing.Image) ("Button3.image")); this.button3.location = new system.drawing. Point (253, 148); this.button3.name = "button3"; this.button3.size = new system.drawing.size (32, 32); this.button3.tabindex = 3; //// Label1 // THISLABEL1.LOCATION = New System.drawing.point (149, 60); this.label1.name = "label1"; this.label1.size = new system.drawing.size (32, 23); this.label1. TabINDEX = 4; this.label1.text = "stop"; /// Label2 // this.label2.Location = new system.drawing.point (149, 108); this.label2.name = "label2"; this .label2.s Ize = new system.drawing.size (40, 23); this.label2.tabindex = 5; this.label2.text = "pause"; /// Label3 // this.label3.Location = new system.drawing. Point (149, 156); this.label3.name = "label3"; this.label3.size = new system.drawing.size (96, 23); this.Label3.TabINDEX = 6; this.label3.text = " Start / continue "; // // ComboBox1 // this.comboBox1.location = new system.drawing.point (128, 24); this.comboBox1.name ="

ComboBox1 "; this.comboBox1.size = new system.drawing.size (160, 20); this.comboBox1.tabindex = 7; this.comboBox1.text =" ydglserver "; // // form1 // this.autoscaleBaseSize = New system.drawing.size (6, 14); this.clientsize = new system.drawing.size (392, 237); this.Controls.add (this.comboBox1); this.controls.add (this.label3); This.Controls.add (this.label2); this.controls.add (this.label1); this.controls.add (this.button3); this.controls.add (this.button2); this.controls.add ( this.button1); this.controls.add (this.picturebox1); this.name = "form1"; this.startPosition = system.Windows.FormStartPosition.centerscreen; this.text = "Power Marketing Application Server"; THIS .Load = new system.eventhandler (this.form1_load); this.ResumeLayout (false);} #ENDREGION / /

/// Application main entry point. /// [STAThread] static void Main () {Application.Run (new Form1 ());} private void Form1_Load (object sender, System.EventArgs e) {// registered channels TcpChannel chan = new TcpChannel (8085); ChannelServices. RegisterChannel (chan); // Register the remote object RemotingConfiguration.RegisterWellKnownServiceType provide services (typeof (ydglDB), "ydglDB", WellKnownObjectMode.Singleton);}}} step 3: create remote client ResumeClinet us for the above Created YDGLSERVER remote objects are created. To create this project, click "File" -> "Create" -> "Project" in turn, then select Create a Windows Application Type, the name of YDGL.

As in the second step, we need to add a reference to the DLL file created in the first step and System.Runtime.Remoting DLL. Both the following code is especially important for .NET Remoting. The first line creates a TCP client channel that is not binding on a port; the second row gets a reference to the remote ResumeAder object. using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime .Remoting.channels.tcp; using ydglserClassLibrary1; Namespace WinUI {///

/// Form1 summary description.

/// public class Form1: System.Windows.Forms.Form {private System.Windows.Forms.MainMenu mainMenu1; private System.Windows.Forms.MenuItem menuItem1; private System.Windows.Forms.MenuItem menuItem2; private System.Windows.Forms.MenuItem menuItem3; private System.Windows.Forms.MenuItem menuItem4; private System.Windows.Forms.MenuItem menuItem5; private System.Windows.Forms.MenuItem menuItem6; private System.Windows.Forms.MenuItem menuItem7; private System.Windows.Forms.MenuItem menuItem8; private System.Windows.Forms.MenuItem menuItem9; private System.Windows.Forms.MenuItem menuItem10; private System.Windows.Forms.MenuItem menuItem11; private System.Windows.Forms.ToolBar toolBar1; private System.Windows.Forms.StatusBar statusBar1; private System.Windows.Forms.ToolBarButton toolBarButton1; private System.Windows.Forms.ToolBarButton toolBarButton2; private System.Windows .Forms.ToolBarButton toolBarButton3; private System.Windows.Forms.ToolBarButton toolBarButton4; private System.Windows.Forms.ToolBarButton toolBarButton5; private System.Windows.Forms.ImageList imageList1; private System.Windows.Forms.DataGrid dataGrid1; private System.Windows .Forms.Button button1; private System.ComponentModel.IContainer components; private DataSet ds1; private System.Windows.Forms.Button button2; private ydglDB ydglDB1; public Form1 () {// // Windows Forms designer support necessary // InitializationComponent (); // // Todo: Add any constructor code after the initializationComponent call //} ///

/// Clean all the resources being used. /// Protected Override Void Dispose (Bool Disposing) {if (disponents! = Null) {Components.dispose ();}} Base.Dispose (DISPOSION);} #Region Windows Form The code generated by the designer ///

/// designer supports the required method - do not use the code editor to modify the // / this method.

/// private void InitializeComponent () {this.components = new System.ComponentModel.Container (); System.Resources.ResourceManager resources = new System.Resources.ResourceManager (typeof (Form1)); this.mainMenu1 = New system.windows.Forms.mainMenu (); this.Menuitem1 = new system.windows.forms.MenuItem (); this.Menuitem6 = new system.windows.forms.MenuItem (); this.Menuitem7 = new System.Windows. Forms.Menuitem (); this.Menuitem9 = new system.windows.forms.Menuitem (); this.Menuitem8 = new system.windows.forms.MenuItem (); this.Menuitem2 = new system.windows.forms.MenuItem () This.Menuitem3 = new system.windows.Forms.Menuitem (); this.Menuitem4 = new system.windows.forms.MenuItem (); this.Menuitem5 = new system.windows.forms.Menuitem (); this.Menuitem10 = New system.windows.Forms.MenuItem (); this.Menuitem11 = new system.windows.forms.MenuItem (); this.toolBar1 = new System.Windows.Forms.ToolBar (); this.toolBarButton1 = new System.Windows.Forms.ToolBarButton (); this.toolBarButton2 = new System.Windows.Forms.ToolBarButton (); this.toolBarButton3 = new System.Windows.Forms.ToolBarButton (); this.toolBarButton4 = new System.Windows.Forms.ToolBarButton (); this.toolBarButton5 = new System.Windows.Forms.ToolBarButton (); this.imageList1 = new System.Windows.Forms = New System.Windows.Forms.statusbar (); this.dataGrid1 = new system.windows.forms.dataGrid ();

This.Button1 = new system.windows.Forms.Button (); this.button2 = new system.windows.Forms.Button (); ("This.DataGrid1)). Beginit (); this .Suspendlayout (); ///minmenu1 // this.mainMenu1.Menuitems.addrange (new system.windows.forms.Menuitem [] {this.Menuitem1, this.Menuitem2, this.Menuitem5}); /// Menuitem1 // this.Menuitem1.index = 0; this.Menuitem1.Menuitems.addrange (new system.windows.forms.Menuitem [] {this.Menuitem6, this.Menuitem7, this.Menuite M9, this.Menuitem8}; this.Menuitem1.text = "& f file"; // // Menuitem6 // this.Menuitem6.index = 0; this.MenuItem6.text = "Application Server Configuration"; // // Menuitem7 // this.Menuitem7.index = 1; this.Menuitem7.text = "Login"; ///muitem9 // this.Menuitem9.index = 2; this.Menuitem9.Text = "-"; ///// Menuitem8 // this.Menuitem8.index = 3; this.Menuitem8.Text = "Exit";

///Menuitem2 // this.Menuitem2.index = 1; this.Menuitem2.Menuitems.addrange (new system.windows.Forms.Menuitem [] {this.Menuitem3, this.Menuitem4}; this.Menuitem2.text = "& W window"; // // Menuitem3 /// this.Menuitem3.index = 0; this.Menuitem3.text = "horizontally"; this.Menuitem3.click = new system.eventhandler (this.Menuitem3_click); // / / menuItem4 // this.menuItem4.Index = 1; this.menuItem4.Text = "vertical"; // // menuItem5 // this.menuItem5.Index = 2; this.menuItem5.MenuItems.AddRange (new System.Windows. Forms.MenuItem [] {this.Menuitem10, this.me Nuitem11}; this.Menuitem5.text = "& h Help"; // // Menuitem10 // this.Menuitem10.index = 0; this.Menuitem10.Text = "Help"; this.Menuitem10.click = new system. EventHandler (this.Menuitem10_Click); /// Menuitem11 // this.Menuitem11.index = 1; this.Menuitem11.Text = "About Power Marketing Management System ...";

// // toolBar1 // this.toolBar1.Buttons.AddRange (new System.Windows.Forms.ToolBarButton [] {this.toolBarButton1, this.toolBarButton2, this.toolBarButton3, this.toolBarButton4, this.toolBarButton5}); this. toolBar1.DropDownArrows = true; this.toolBar1.ImageList = this.imageList1; this.toolBar1.Location = new System.Drawing.Point (0, 0); this.toolBar1.Name = "toolBar1"; this.toolBar1.ShowToolTips = True; this.toolbar1.size = new system.drawing.size (1016, 28); this.toolbar1.tabindex = 2; this.toolbar1.buttonclick = new system.windows.Forms.toolbarButtonclicKeventhand ler (this.toolBar1_ButtonClick); // // toolBarButton1 // this.toolBarButton1.ImageIndex = 0; // // toolBarButton2 // this.toolBarButton2.ImageIndex = 1; // // toolBarButton3 // this.toolBarButton3.ImageIndex = 5; // // toolBarButton4 // this.toolBarButton4.ImageIndex = 4; // // toolBarButton5 // this.toolBarButton5.ImageIndex = 3; // // imageList1 // this.imageList1.ColorDepth = System.Windows.Forms .Colordepth.depth32bit;

this.imageList1.ImageSize = new System.Drawing.Size (16, 16); this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer) (resources.GetObject ( "imageList1.ImageStream"))); this. imageList1.TransparentColor = System.Drawing.Color.Transparent; // // statusBar1 // this.statusBar1.Location = new System.Drawing.Point (0, 699); this.statusBar1.Name = "statusBar1"; this.statusBar1 .Size = new system.drawing.size (1016, 22); this.statusbar1.tabindex = 3; this.statusbar1.text = "statusbar1"; // // DataGrid1 // this.dataGrid1.datamember = ";" .dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.dataGrid1.Location = new System.Drawing.Point (304, 96); this.dataGrid1.Name = "dataGrid1"; this.dataGrid1.Size = new System. Drawing.size (416, 160); this.DataGrid1.tabindex = 5; this.DataGrid1. Navigate = new System.Windows.Forms.NavigateEventHandler (this.dataGrid1_Navigate); // // button1 // this.button1.Location = new System.Drawing.Point (488, 312); this.button1.Name = "button1 "; This.button1.tabindex = 7; this.Button1.text =" update "; this.button1.click = new system.eventhandler (this.button1_click); // // Button2 // this.button2.location = New system.drawing.point (312, 312); this.button2.name = "button2"; this.button2.tabindex =

9.Button2.text = "retrieve"; this.button2.click = new system.eventhandler (this.button2_click); // // form1 // this.autoscalebasesize = new system.drawing.size (6, 14 ); This.drawing.systemcolors.drawing.systemcolors.drof; this.clientsize = new system.drawing.size (1016, 721); this.controls.add (this.button2); this.Controls.add (this.button1 This.Controls.add (this.DataGrid1); this.Controls.add (this.statusbar1); this.controls.add (this.toolbar1); this.Mdicontainer = true; this.Menu = this.mainu1; this. .Name = "Form1"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "power marketing management"; this.WindowState = System.Windows.Forms.FormWindowState.Maximized; this.Load = NEW System.EventHandler (this.form1_load); (System.comPonentModel.isupportInitialize) (this.DataGrid1)). Endinit (); this.ResumeLayout (false);} #ENDREGION / /

/// The primary entry point of the application.

转载请注明原文地址:https://www.9cbs.com/read-79733.html

New Post(0)