ARTICLE

Accessing Rmote SQL server by Remoting (TCP)

Posted by John Jiang Articles | SQL Server 2012 March 31, 2008
This article describes how to build a distribution application that accesses MS SQL Server 2000 with a remoting service.
Reader Level:
Download Files:
 

Introduction:

In this sample describes how to build a distribution application which accesses MS SqlServer2000 on remoting service.

Using the code

Server

using System;

using System.Data;

using System.Data.SqlClient;

 

namespace www.treaple.com

{

    public class Hello : System.MarshalByRefObject

    {

        public Hello()

        {

            Console.WriteLine("Hello actived");

        }

 

        ~Hello()

        {

            Console.WriteLine("Hello destroyed");

        }

 

        public DataSet GetData()

        {

            string conn = "Initial Catalog=Northwind;Data Source=localhost;Integrated Security=SSPI";//newSqlConnectionSystem.Configuration.ConfigurationSettings.AppSettings["strconn"]);
           
SqlDataAdapter da = new SqlDataAdapter("select * from Products", conn);

            DataSet ds = new DataSet();

            da.Fill(ds);

            return ds;

        }

    }
}

Client

using System;

using System.Runtime.Remoting;

using System.Runtime.Remoting.Channels;

using System.Runtime.Remoting.Channels.Tcp;

using System.Data;

 

namespace www.treaple.com

{

    public class Client

    {

        [STAThread]

        public static void Main(string[] args)

        {

            //TcpChannel chan = new TcpChannel();

            ChannelServices.RegisterChannel(new TcpClientChannel());

            Hello obj = (Hello)Activator.GetObject(typeof(Hello), "tcp://localhost:8085/Hi");

            DataTable dt = obj.GetData().Tables[0];

            foreach (DataRow dr in dt.Rows)

            {

                Console.WriteLine(dr["ProductID"] + " " + dr["ProductName"]);

            }

            //if (obj == null) System.Console.WriteLine("Could not find server");

            //else Console.WriteLine(obj.Greeting("kk"));

            //else Console.WriteLine(obj.HelloMethod("John"));

 

        }

    }

}

Interface:

using System;

 

namespace www.treaple.com

{

    public interface IHello

    {

        String HelloMethod(String name);

    }

 

    public class HelloServer : MarshalByRefObject, IHello

    {

        public HelloServer()

        {

            Console.WriteLine("HelloServer actived");

        }

 

        public String HelloMethod(String name)

        {

            Console.WriteLine("Hello.HelloMethod : {0}", name);

            return "hello" + name;

        }

    }
}

Login to add your contents and source code to this article
post comment
     
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Get Career Advice from Experts
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter