Wednesday, January 05, 2011

REMOTING using SQL Server database

Well, this is a very simple Example to implement the remoting concept.

It consists of 3 parts:
Business Component.
Server application
Client application

I have used the northwind database of the SQL Server in the business logic

Business Component
//pwd is 1234: Use the password  of your system. Northwind database must be Installed
//You can specify the IP Address of the server computer in place of.

Open a class library project .Name it as remlibrary.
Type this code in the class file.


using System;
using System.Data;
using System.Data.SqlClient;
public class product:MarshalByRefObject
{
 public DataSet GetDataFromDatabase(int catID)
 {
     SqlConnection cn = new SqlConnection("server=.;uid=sa;pwd=1234;database=northwind");
     SqlDataAdapter da = new SqlDataAdapter("select productname,unitprice from products where categoryid=@a", cn);
     da.SelectCommand.Parameters.AddWithValue("@a", catID);
     DataSet ds = new DataSet();
     da.Fill(ds, "abc");
     return ds;
 }

}
Server application:
Open a Console Application.Name it as ConsoleApplicationrem.
Add a reference to System.RunTime.Remoting.dll
//Note: The port number is 6 for the TCP protocol.
//8085 or other port numbers are just fictitious port numbers.
//For implementing on 2 different machines, the port number must be 6 using TCP
using System;
//using System.IO;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace ConsoleApplicationrem
{
public class Server
{
 public static void Main()
 {
     TcpChannel h=new TcpChannel(6);
   ChannelServices.RegisterChannel(h,false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(product),
"RemoteObjects",WellKnownObjectMode.Singleton);
   Console.WriteLine("The Server hasstarted");
   Console.WriteLine("Press the enter keyto stop the server ...");
   Console.ReadLine();
 }
}
}
For the Client application.

Read more: C# Corner