Reader Level:
Articles

REMOTING Using SQL Server Database

By Dinesh Dugg on January 04, 2011
In this article you will learn how to use REMOTING in SQL Server database.
  • 0
  • 0
  • 13944
Download Files:
 

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 mchine.

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.

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 a 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

// You must enable the SQL Server For Remote Connections on the server machine

//if you are using 2 machines.

using System;

//using System.IO;

using System.Runtime.Remoting;

using System.Runtime.Remoting.Channels;

using System.Runtime.Remoting.Channels.Tcp;
using remlibrary;
 

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.

You must add a reference to the remlibrary.dll;
 

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;

using remlibrary;

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];

        }

    }

}

Dinesh Dugg

MCSD in .NET,  8 years Experince as a .net Trainer.

COMMENT USING

Trending up