REMOTING Using SQL Server Database

Well, this is a very simple Example to implement the remoting concept.
 
It consists of 3 parts: 
  1. Business Component.
  2. Server application
  3. Client application
I have used the Northwind database of the SQL Server in the business logic layer. The code works OK.
 
How to execute?
  1. Copy the server folder on one Windows XP SP2 Machine
  2. Copy the client folder on the same Windows XP SP2 machine
  3. Click the exe in the server folder
  4. Click the exe in the Client folder.
 The Client exe will execute as long as the server exe is running. The moment the server exe is stopped, the client will generate an exception.
 
//Note: if you are using 2 machines, specify the  IP address of the server machine in place of the localhost.
 
For 2 different machines, copy the client folder to one machine, and the server folder to the other machine.
 
Business Component
 
//pwd is 1234: Use the password of your system. The Northwind database must be installed.
 
//You can specify the IP Address of the
//server computer in place of. 
  1. Open a class library project.Name it as remlibrary.
  2. Type this code in the class file.
  1. using System;  
  2. using System.Data;  
  3. using System.Data.SqlClient;  
  4. public class product:MarshalByRefObject  
  5. {  
  6.   public DataSet GetDataFromDatabase(int catID)  
  7.   {  
  8.       SqlConnection cn = new SqlConnection("server=.;uid=sa;pwd=1234;database=northwind");  
  9.       SqlDataAdapter da = new SqlDataAdapter("select productname,unitprice from products where categoryid=@a", cn);  
  10.       da.SelectCommand.Parameters.AddWithValue("@a", catID);  
  11.       DataSet ds = new DataSet();  
  12.       da.Fill(ds, "abc");  
  13.       return ds;  
  14.   }  

Server application:
  1. Open a Console Application.Name it as ConsoleApplicationrem.
  2. Add a reference to System.RunTime.Remoting.dll
  3. Add a reference to the remlibrary.dll
  1. //Note: The port number is 6 for the TCP protocol.  
  2. //8085 or other port numbers are just fictitious port numbers.  
  3. //For implementing on 2 different machines, the port number must be 6 using TCP  
  4.   
  5. // You must enable the SQL Server For Remote Connections on the server machine  
  6.   
  7. //if you are using 2 machines.  
  8.   
  9. using System;  
  10. //using System.IO;  
  11. using System.Runtime.Remoting;  
  12. using System.Runtime.Remoting.Channels;  
  13. using System.Runtime.Remoting.Channels.Tcp;  
  14. using remlibrary;  
  15. namespace ConsoleApplicationrem {  
  16.     public class Server {  
  17.         public static void Main() {  
  18.             TcpChannel h = new TcpChannel(6);  
  19.             ChannelServices.RegisterChannel(h, false);  
  20.             RemotingConfiguration.RegisterWellKnownServiceType(typeof(product),  
  21.                 "RemoteObjects", WellKnownObjectMode.Singleton);  
  22.             Console.WriteLine("The Server hasstarted");  
  23.             Console.WriteLine("Press the enter keyto stop the server ...");  
  24.             Console.ReadLine();  
  25.         }  
  26.     }  

For the Client application.
 
You must add a reference to the remlibrary.dll; 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using remlibrary;  
  10. namespace WindowsFormsApplicationrem  
  11. {  
  12.     public partial class Form1 : Form  
  13.     {  
  14.          
  15.         public Form1()  
  16.         {  
  17.             InitializeComponent();  
  18.         }  
  19.         private void button1_Click(object sender, EventArgs e)  
  20.         {  
  21.            
  22.             product remoteObject = (product)Activator.GetObject(typeof  
  23.               (product), "tcp://localhost:6/RemoteObjects");  
  24.             int a=Convert.ToInt32(textBox1.Text);  
  25.             DataSet ds=remoteObject.GetDataFromDatabase(a);  
  26.             dataGridView1.DataSource = ds.Tables[0];  
  27.         }  
  28.     }  
  29. }