I have used the northwind database of the SQL Server in the business logic layer. The code works OK.
How to execute?
-
Copy the server folder on one Windows XP SP2 Machine
-
Copy the client folder on the same Windows XP SP2 machine
-
Click the exe in the server folder
-
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.
-
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
-
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];
}
}