Remoting using sql server database

Posted by Ddd under Windows Forms category on | Points: 40 | Views : 3218
CODE REMOTING using SQL Server database

Dear All,

Well, this is a very simple Example to implement the remoting concept using SQL Server database

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)Click on the exe in the server application.

2)Click on the exe in the client application.

The Client exe will execute as long as the server exe is running. The moment server exe is stopped, 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.

Business Component

//pwd is 1234: Use the password of your system. Northwind database must be


//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. Name the class as product

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:

1)Open a Console Application.Name it as

2)Add a reference to System.RunTime.Remoting.dll

3) Add reference to the remlibrary 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.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);
Console.WriteLine("The Server hasstarted");
Console.WriteLine("Press the enter keyto stop the server ...");

For the Client application.

1)Open a windows application. Name it as WindowsApplicationrem

Paste a DataGridView, TextBox and a button on the form.

2) Add reference to the remlibrary dll

In the Code Window (Form1.cs), the contents should be like this.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplicationrem
public partial class Form1 : Form

public Form1()

private void button1_Click(object sender, EventArgs e)

product remoteObject = (product)Activator.GetObject(typeof
(product), "tcp://localhost:6/RemoteObjects");
int a=Convert.ToInt32(textBox1.Text);
DataSet ds=remoteObject.GetDataFromDatabase(a);

dataGridView1.DataSource = ds.Tables[0];


1)For2 different machines, copy the remlibrary.dll and ConsoleApplicationrem.exe on one machine
and remlibrary.dll and WindowsApplicationrem.exe on one machine

2)First configure the SQL Server 2005 for Remote Connections(if you are using 2 machines) .This is on Server machine.

It is very easy

Follow these steps:

Programs--->Microsoft SQL Server 2005-->Configuration Tools--> SQL Server Surface Area Configuration-->

Surface Area Configuration for Services and Connections-->Click Database Engine-->Remote Connections-->

Local and Remote Connections--->Using both TCP/IP and Named Pipes

Click the ConsoleApplicationrem.exe and then the WindowsApplicationrem.exe

Comments or Responses

Posted by: Chvrsri on: 1/4/2011 Level:Silver | Status: [Member] [Moderator] [MVP] | Points: 10

Very Nice ,

But small suggestion kindly format the resorce by placing the code section in the code block. So it looks more organized.
Posted by: T.saravanan on: 1/4/2011 Level:Silver | Status: [Member] [MVP] | Points: 10
Hi Ddd,

Very nice code.
Small suggestion to you,Kindly post your code using Code Tag.

Login to post response