TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
John Jiang
NA
410
209.2k
Accessing remote SQL server by .net remoting(TCP)
Mar 27 2008 11:45 AM
Accessing remote SQL server by remoting(TCP)
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";
//new SqlConnection(System.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;
}
}
}
Note:
If you have any questions about this sample or want to download the source code of it.please visit
http://www.treaple.com/bbs/thread-28-1-1.html
Reply
Answers (
0
)
Simple Threading
Generating a Random Number