Click here to Skip to main content
15,885,278 members
Articles / Database Development / SQL Server
Tip/Trick

REMOTING using SQL Server

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
7 Jan 2011CPOL2 min read 12.5K   6  
Example to implement remoting concept using SQL Server database
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 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. Name the class as product.

C#
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 ConsoleApplicationrem.
2)Add a reference to System.RunTime.Remoting.dll.
3) Add reference to the remlibrary DLL.

C#
//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); 
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:

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.

C#
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() 
{ 
InitializeComponent(); 
} 
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]; 
} 
} 
} 


Note:
1)For 2 different machines, copy the remlibrary.dll and ConsoleApplicationrem.exe on one machine and remlibrary.dll and WindowsApplicationrem.exe on the other 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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Instructor/Trainer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --