新聞中心
在分布式系統(tǒng)中,遠程對象通信技術(shù)實現(xiàn)了不同計算機上的應(yīng)用程序進行通信和交互。Remoting就是一種遠程對象通信技術(shù)。在開發(fā)這樣的分布式系統(tǒng)時,我們經(jīng)常需要訪問數(shù)據(jù)庫里面的數(shù)據(jù)并進行操作,那么如何呢?本文將詳細介紹如何使用Remoting技術(shù)實現(xiàn)數(shù)據(jù)庫訪問。

黑龍江ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:13518219792(備注:SSL證書合作)期待與您的合作!
一、什么是Remoting技術(shù)?
Remoting技術(shù)是一種基于.NET的遠程對象通信技術(shù),可以讓一個對象在一個應(yīng)用程序域中向另一個應(yīng)用程序域中的對象發(fā)送消息。這種技術(shù)需要至少兩個應(yīng)用程序域之間的通信。
Remoting使用兩個對象,一個是客戶端對象,一個是服務(wù)器端對象,客戶端對象根據(jù)需要創(chuàng)建服務(wù)器端對象并向其發(fā)送消息,服務(wù)器端對象接收消息并響應(yīng)。這種技術(shù)可以在兩個應(yīng)用程序域之間直接傳送對象和數(shù)據(jù)。
二、如何使用Remoting技術(shù)訪問數(shù)據(jù)庫?
1. 創(chuàng)建Remoting服務(wù)
首先需要創(chuàng)建Remoting服務(wù)。創(chuàng)建一個Remoting服務(wù)的過程包括:
(1)創(chuàng)建遠程接口
遠程接口是客戶端對象將要調(diào)用的遠程方法的??蛻舳送ㄟ^這些方法來訪問遠程對象。在這個例子中,我們需要一個接口來定義訪問數(shù)據(jù)庫的遠程方法。
(2)創(chuàng)建遠程對象
遠程對象是實現(xiàn)遠程接口的類的實例。遠程對象包含在服務(wù)器端應(yīng)用程序中,它是由客戶端對象創(chuàng)建的,然后通過遠程接口來訪問。
2.使用Remoting訪問數(shù)據(jù)庫
接下來,我們需要在服務(wù)器端應(yīng)用程序中使用Remoting來訪問數(shù)據(jù)庫。主要包括:
(1)創(chuàng)建數(shù)據(jù)庫連接
在服務(wù)器端應(yīng)用程序中,我們需要使用ADO.NET來創(chuàng)建一個數(shù)據(jù)庫連接。
(2)創(chuàng)建命令對象和處理數(shù)據(jù)
通過ADO.NET,我們可以使用SqlCommand和SqlDataAdapter類來創(chuàng)建命令對象和處理數(shù)據(jù)。使用SqlCommand類可以執(zhí)行SQL語句,并將結(jié)果放入DataSet對象中。
(3)返回數(shù)據(jù)結(jié)果
服務(wù)器端對象負責(zé)執(zhí)行SQL語句,并將數(shù)據(jù)結(jié)果返回給客戶端對象。客戶端對象可以使用返回的數(shù)據(jù)填充數(shù)據(jù)表格或數(shù)據(jù)集。
3.在客戶端訪問Remoting服務(wù)
當(dāng)我們完成上述步驟后,我們可以大致確定如何在服務(wù)器端中使用Remoting來訪問數(shù)據(jù)庫。接下來需要在客戶端中使用Remoting來調(diào)用遠程方法并獲取數(shù)據(jù)。主要包括以下步驟:
(1)創(chuàng)建一個客戶端應(yīng)用程序
客戶端應(yīng)用程序負責(zé)創(chuàng)建遠程對象,然后調(diào)用遠程方法。
(2)創(chuàng)建遠程代理對象
創(chuàng)建遠程代理對象,然后使用它來調(diào)用遠程方法。可以使用.Net的Activator來創(chuàng)建遠程代理對象。
(3)通過遠程對象獲取數(shù)據(jù)
客戶端通過遠程對象調(diào)用遠程方法來獲取服務(wù)器端的數(shù)據(jù)。
三、
在本文中,我們介紹了的步驟。首先創(chuàng)建一個Remoting服務(wù),包括創(chuàng)建遠程接口和遠程對象。接下來在服務(wù)器端應(yīng)用程序中使用Remoting訪問數(shù)據(jù)庫,并將結(jié)果返回給客戶端對象。客戶端對象可以通過遠程代理對象調(diào)用遠程方法獲取服務(wù)器端的數(shù)據(jù)。這樣我們就可以使用Remoting技術(shù)來實現(xiàn)數(shù)據(jù)庫訪問。
成都網(wǎng)站建設(shè)公司-創(chuàng)新互聯(lián),建站經(jīng)驗豐富以策略為先導(dǎo)10多年以來專注數(shù)字化網(wǎng)站建設(shè),提供企業(yè)網(wǎng)站建設(shè),高端網(wǎng)站設(shè)計,響應(yīng)式網(wǎng)站制作,設(shè)計師量身打造品牌風(fēng)格,熱線:028-86922220要通過webserver訪問遠程數(shù)據(jù)庫的內(nèi)容返回數(shù)據(jù)集,并存到本地數(shù)據(jù)庫。要怎么做呀。
我不知道你的webserver是個什么東西,我暫時把他當(dāng)做remoting或者webservice吧,在客戶端調(diào)用webserver,webserver查詢數(shù)據(jù)庫,查詢結(jié)果轉(zhuǎn)換成XML,傳回客戶端,客戶單端處理一下插入數(shù)據(jù)庫就可以了吧,希望我說的對你有幫助
.net Remoting編程-客戶端訂閱服務(wù)端事件
之一步:創(chuàng)建共享庫
依次點擊“文件”->“新創(chuàng)建”->“工程”,選慎正擇創(chuàng)建一個c# library,并將其命名為resumeserverlibrary,然后點擊ok按鈕。這將創(chuàng)建一個我們的.net remote客戶端和服務(wù)器端用來通訊的“共享命令集”。
正面是完整的代碼,如果要跳過數(shù)據(jù)庫訪問部分,可以使用下面的代碼替換春孝陵resumeloader對象:
public class resumeloader : system.marshalbyrefobject
{
public resumeloader()
{
system.console.writeline(“new referance added!”);
}
public resume getresumebyuserid(decimal userid)
{
return new resume(1);
}
}
名字空間是對象所需要的。請記住,如果得到system.runtime.remoting.channels.tcp名字空間不存在的信息,請檢查是否象上面的代碼那樣添加了對system.runtime.remoting.dll的引用。
using system;
using system.runtime;
using system.data.sqlclient;
我們?yōu)閷ο笫褂玫拿挚臻g是dotnetremotetest,下面的對象是marshalbyrefobject,在其中我們創(chuàng)建了一個引用和包括服務(wù)器端數(shù)據(jù)庫操作全部完成所需要的扒戚所有工作。
namespace dotnetremotetest
{
public class resumeloader : system.marshalbyrefobject
{
private sqlconnection dbconnection;
public resumeloader()
{
this.dbconnection = new system.data.sqlclient.sqlconnection();
this.dbconnection.connectionstring =
”data source=grimsaado2k;initial catalog=underground;integrated security=sspi;pers” +
”ist security info=true;workstation id=grimsaado2k;packet size=4096″;
/*具體的連接字符串會有所不同,這超出了本篇文章的范圍。如果不清楚如何創(chuàng)建一個數(shù)據(jù)庫連接,請使用這一對象的另一個版本。*/
system.console.writeline(“new referance added!”);
}
public resume getresumebyuserid(decimal userid)
{
resume resume = new resume();
try
{
dbconnection.open();
sqlcommand cmd = new sqlcommand(
”select resumeid, userid, title, body from resume as theresume where theresume.userid=”+ userid +””
, dbconnection
);
sqldatareader areader = cmd.executereader();
if(areader.read())
{
resume.resumeid=areader.getdecimal(0);
resume.userid=areader.getdecimal(1);
resume.title=areader.getstring(2);
resume.body=areader.getstring(3);
}
areader.close();
dbconnection.close();
}
catch(exception x) { resume.title=”error:”+x; }
return resume;
}
}
resume需要能夠被串行化,以便能作為被遠程調(diào)用的.net remote對象的返回類型,原因是該對象將被轉(zhuǎn)換為通過網(wǎng)絡(luò)傳輸?shù)脑紨?shù)據(jù),然后在網(wǎng)絡(luò)的另一端再被裝配成一個對象。
該對象非常簡單,為了使本篇文章看起來更簡單,其中的構(gòu)造器甚至使用缺省的內(nèi)容初始化其中的一些域。
public class resume
{
private decimal resumeid, userid;
private string body, title;
public resume(decimal resumeid)
{
this.resumeid=resumeid;
this.userid=1;
this.body=”this is the default body of the resume”;
this.title=”this is the default title”;
}
public decimal resumeid
{
get { return resumeid; }
set { this.resumeid=value; }
}
public decimal userid
{
get { return userid; }
set { this.userid=value; }
}
public string body
{
get { return body; }
set { this.body=value;}
}
public string title
{
get { return title; }
set { this.title=value; }
}
}//resume對象結(jié)束
}//dotnetremotetest名字空間結(jié)束
編譯創(chuàng)建的工程,就會得到一個dll文件,并可以在其他的工程中使用它。
第二步:創(chuàng)建server對象
有幾種方法可以創(chuàng)建server對象,最直觀的方法是下面的方法:在visual studio.net中,依次點擊“文件”->“新創(chuàng)建”->“工程”,選擇創(chuàng)建一個“command line application”(命令行應(yīng)用程序),并將它命名為resumesuperserver。
最最重要的是,我們需要添加對剛才在之一步中所創(chuàng)建的dll文件的應(yīng)用,該應(yīng)用程序才能正確地運行。依次點擊“工程”->“添加引用”,然后通過點擊“瀏覽”按鈕添加一個對在之一步中所創(chuàng)建的dll文件的引用。
為了使用.net remote功能,必須通過選擇“工程”->“添加引用”,添加對dll文件的引用。在.net標(biāo)簽中選擇system.runtime.remoting.dll,然后點擊“ok”按鈕。然后,需要象我們在之一步中那樣添加對system.runtime.remoting.dll的引用。
下面的對象相當(dāng)?shù)暮唵魏椭庇^,我將就真正與.net remoting相關(guān)的3行代碼中的每一行進行解釋。
tcpserverchannel是.net remoting支持的二種信道類型中的一種,它將設(shè)置我們希望我們的對象對來自哪一個端口的請求進行回應(yīng),channelservices.registerchannel將把該端口號與操作系統(tǒng)中的tcp/ip棧綁定。
tcpserverchannel channel = new tcpserverchannel(9932);
channelservices.registerchannel(channel);
另一種可以設(shè)置的信道類型是http,只要簡單地使用system.runtime.remoting.channels.http名字空間中的httpserverchannel對象即可搞定。使用http和tcp信道之間的區(qū)別可以簡單的歸結(jié)為:如果應(yīng)用程序是在局域網(wǎng)上運行,則更好使用tcp信道,因為它的性能要好于http信道;如果應(yīng)用程序是在互聯(lián)網(wǎng)上運行,則有時候根據(jù)防火墻的配置,http是唯一的選擇。需要記住的是,如果使用了防火墻軟件,則防火墻應(yīng)該配置成允許tcp數(shù)據(jù)流量通過你為對象選擇的端口。
remotingconfiguration.registerwellknownservicetype(typeof(resumeloader),
”resumeloader”, wellknownobjectmode.singlecall);
這行代碼設(shè)置了服務(wù)中的一些參數(shù)和把欲使用的對象名字與遠程對象進行綁定,之一個參數(shù)是綁定的對象,第二個參數(shù)是tcp或http信道中遠程對象名字的字符串,第三個參數(shù)讓容器知道,當(dāng)有對對象的請求傳來時,應(yīng)該如何處理對象。盡管wellknownobjectmode.single對所有的調(diào)用者使用一個對象的實例,但它為每個客戶生成這個對象的一個實例。
完整的對象代碼如下所示:
using system;
using system.runtime;
using system.runtime.remoting;
using system.runtime.remoting.channels;
using system.runtime.remoting.channels.tcp;
using system.data.sqlclient;
using dotnetremotetest;
namespace resumeserverserver
{
public class resumesuperserver
{
public static void main(string args)
{
tcpserverchannel channel = new tcpserverchannel(9932);
channelservices.registerchannel(channel);
remotingconfiguration.registerwellknownservicetype(typeof(resumeloader),
”resumeloader”, wellknownobjectmode.singlecall);
system.console.writeline(“press any key”);
system.console.readline();
}
}
}
編譯這一程序并注意生成的.exe文件的位置。
第三步:創(chuàng)建remote客戶端程序
resumeclinet是我們?yōu)閷υ谏厦鎰?chuàng)建的resumesuperserver遠和對象進行測試而創(chuàng)建的。要創(chuàng)建這一工程,可以依次點擊“文件”->“創(chuàng)建”->“工程”,然后選擇創(chuàng)建一個console application類型、名字為resumeclient的工程名。象在第二步中那樣,我們需要添加對在之一步中創(chuàng)建的dll文件和system.runtime.remoting dll的引用。
下面的代碼中有二行對于.net remoting而言是特別重要的。之一行創(chuàng)建了一個tcp客戶端信道,該信道并不是綁定在一個端口上的;第二行獲取了一個對遠程的resumeloader對象的引用。activator.getobject方法返回一個對象類型的值,我們隨后會將它返回的值賦予resumeloader。我們傳給它的參數(shù)與在服務(wù)器工程中傳遞給remotingconfiguration的參數(shù)非常地相似,之一個參數(shù)是對象類型的,第二個參數(shù)是遠程對象的uri。
channelservices.registerchannel(new tcpclientchannel());
resumeloader loader = (resumeloader)activator.getobject(
typeof(resumeloader), ”
resumeclient的全部代碼如下所示:
using system;
using system.runtime.remoting;
using system.runtime.remoting.channels;
using system.runtime.remoting.channels.tcp;
using dotnetremotetest;
namespace resumeclient
{
public class resumeclient
{
public static void main(string args)
{
channelservices.registerchannel(new tcpclientchannel());
resumeloader loader = (resumeloader)activator.getobject(
typeof(resumeserver), ”
if(rs==null)
{ console.writeline(“unable to get remote referance”); }
else
{
resume resume = loader.getresumebyuserid(1);
console.writeline(“resumeid:”+ resume.resumeid);
console.writeline(“userid:”+ resume.userid);
console.writeline(“title:”+ resume.title);
console.writeline(“body:”+ resume.body);
}
console.readline();//在能夠看到結(jié)果前不讓窗口關(guān)閉
}//end of main method
}//end of resumeclient object
}//end of resumeclientnamespace
測試
在數(shù)據(jù)庫中創(chuàng)建一個具有如下結(jié)構(gòu)的表:
table name-resume
resumeid, numeric (autonumber)
userid, numeric
title, char(30)
body, text
雙擊我們在第二步中創(chuàng)建的server.exe,然后雙擊在第三步中創(chuàng)建的client可執(zhí)行文件。如果一切正常的話,我們應(yīng)該能夠看到數(shù)據(jù)庫中resumeid的值為1的記錄行。
關(guān)于remoting 訪問數(shù)據(jù)庫的介紹到此就結(jié)束了,不知道你從中找到你需要的信息了嗎 ?如果你還想了解更多這方面的信息,記得收藏關(guān)注本站。
成都創(chuàng)新互聯(lián)科技有限公司,經(jīng)過多年的不懈努力,公司現(xiàn)已經(jīng)成為一家專業(yè)從事IT產(chǎn)品開發(fā)和營銷公司。廣泛應(yīng)用于計算機網(wǎng)絡(luò)、設(shè)計、SEO優(yōu)化、關(guān)鍵詞排名等多種行業(yè)!
標(biāo)題名稱:使用Remoting實現(xiàn)數(shù)據(jù)庫訪問(remoting訪問數(shù)據(jù)庫)
網(wǎng)頁網(wǎng)址:http://m.fisionsoft.com.cn/article/dhgodcp.html


咨詢
建站咨詢
