请问有什么做remoting 操作数据库吗,能否提供共享简单源码,小弟想学学,谢谢
解决方案 »
- C#如何清除绘制完的文字
- DevExpress的ComboBoxEdit控件填充
- C#.Net 建立 dll, visual studio 中报错 “;expected”
- 问个菜鸟问题,web里面有缓存.winform里面有吗?要不频繁访问数据库怎么弄呀!
- 如何将DataSet里的多张表分别在水晶报表中显示成对应的页?
- 如何在不改变图片边长的情况下改变图像的分辨率
- 100分,请教这段asp3.0的代码怎么转成c#代码?急~~
- 提供成绩管理功能
- 求大神帮助。Debug Assert failed C:\windows\system32\mfc110d.dll
- 请问有在picturebox上做图的例子吗?
- 怎么用正则表达式提取字符串中的汉字?
- (顶者有分)突然想自己写控件了,请大家给点资源。(书,文章,源码都可)最好是入门级别的。
--------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace db
{
public class DBAccess:MarshalByRefObject
{
private SqlConnection con;
private SqlDataAdapter da;
private DataSet ds;
public DBAccess()
{
Console.Write("构造方法");
} public DataSet GetDateSet(string strSql,string tabName)
{
con = new SqlConnection("server=(local);" + "integrated security=SSPI;" + "database=henry");
da = new SqlDataAdapter(strSql, con);
SqlCommandBuilder builder = new SqlCommandBuilder(da);
ds = new DataSet();
da.Fill(ds,tabName);
return ds;
} public override object InitializeLifetimeService()
{
return null;
}
}
}
--------------------------------------服务器文件
--------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace db
{
class Program
{
static void Main(string[] args)
{
TcpServerChannel channel = new TcpServerChannel(8888);
ChannelServices.RegisterChannel(channel, false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(DBAccess), "Hi", WellKnownObjectMode.Singleton);
Console.WriteLine("回车退出");
Console.ReadLine();
}
}
}
------------------------------------------------------客户端代码 form1
---------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace db
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private DBAccess obj;
private void Form1_Load(object sender, EventArgs e)
{
TcpClientChannel channle = new TcpClientChannel();
obj = (DBAccess)Activator.GetObject(typeof(DBAccess), "tcp://localhost:8888/Hi");
dataGrid1.DataSource = obj.GetDateSet("select * from login", "login");
dataGrid1.DataMember = "login";
}
}
}
------------------------------------------
楼主有能帮我server由控制台程序改为windows应用程序吗.
ChannelServices.RegisterChannel(channel, false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(DBAccess), "Hi", WellKnownObjectMode.Singleton);
Console.WriteLine("回车退出");
Console.ReadLine();
放到load里面
ChannelServices.RegisterChannel(channel, false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(DBAccess), "Hi", WellKnownObjectMode.Singleton);
最好判断一下TcpServerChannel channel是不是等于null