谁能帮我注释一下这三层连接例子的源码?(50分)

  • 主题发起人 主题发起人 MingWord
  • 开始时间 开始时间
M

MingWord

Unregistered / Unconfirmed
GUEST, unregistred user!
先作个声明,本人对三层还未入门,所以大家回答的时候,请尽量想像一下你们当时的情况,用通俗的语句表达出来.<br>这个问题本来是想问三层数据如何连接的,但是问问来问去,还是一样没有答案.一来是自己的问话水平差,二来高手也不屑来回答案笨鸟的这类问题,所以我还是用一个我在网上找到的例子,你们帮我解释一下,或许这样容易理解<br>下面是两个单元文件.<br>unit Unit2;<br>{$WARN SYMBOL_PLATFORM OFF}<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Classes, ComServ, ComObj, VCLCom, DataBkr,<br> &nbsp;DBClient, Project1_TLB{[blue]这个就是那个LIB文件,我一直不明白他是如何来的[/blue]}, DB, ADODB, StdVcl, Provider;<br><br>type<br> &nbsp;TMy2005Class = class(TRemoteDataModule, IMy2005Class)<br>{[blue]Q1:别的DataModule都是这样的TDataModule2 = class(TDataModule)的,但是他的却加上了一个IMy2005Class参数,有什么作用的?,当我按着Ctrl+左键查看时,跳出错误提示,“Object reference not set to an instance of an object”,这又是怎么回事?}[/blue]<br> &nbsp; &nbsp;ADOConnection1: TADOConnection;<br> &nbsp; &nbsp;ADOQuery1: TADOQuery;<br> &nbsp; &nbsp;DataSetProvider1: TDataSetProvider;<br> &nbsp; &nbsp;procedure ADOConnection1Disconnect(Connection: TADOConnection;<br> &nbsp; &nbsp; &nbsp;var EventStatus: TEventStatus);{这个可以看得明白}<br> &nbsp; &nbsp;procedure ADOConnection1ConnectComplete(Connection: TADOConnection;<br> &nbsp; &nbsp; &nbsp;const Error: Error; var EventStatus: TEventStatus);{这个也可以看得明白}<br> &nbsp; &nbsp;procedure RemoteDataModuleCreate(Sender: TObject);{这个看不明白}<br> &nbsp;private<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp;protected<br> &nbsp; &nbsp;class procedure UpdateRegistry(Register: Boolean; const ClassID, ProgID: string); override;<br>[blue]{Q2:这个class又是作什么用的?}[/blue]<br> &nbsp;public<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp;end;<br>Const<br> &nbsp;SConnectionString &nbsp; &nbsp; &nbsp; = 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;'<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+'Jet OLEDB:Database Password=%s;';<br>Var<br>[blue]{Q3:平常这个Var下面应该是DataModule: TDataModule这样的语句,为何这里有的?}[/blue]<br>i:integer;<br>implementation<br><br>uses Unit1;<br><br>{$R *.DFM}<br>//得出连接数据库的文本语句<br>function setadoaccess(mdbpath:string;passwd:string):string;<br>begin<br>mdbpath:=trim(mdbpath);<br>passwd:=trim(passwd);<br>result:=format(SConnectionString,[mdbpath,passwd]);<br>[blue]{Q4:format可以这样用的吗?看来我对Delphi的函数知得甚少,丢人呀,哈。。}[/blue]<br>end;<br><br>class procedure TMy2005Class.UpdateRegistry(Register: Boolean; const ClassID, ProgID: string);<br>begin<br> &nbsp;if Register then<br> &nbsp;begin<br> &nbsp; &nbsp;inherited UpdateRegistry(Register, ClassID, ProgID[blue]);{Q5:这个是继承怎么回事,说说}[/blue] &nbsp; &nbsp;EnableSocketTransport(ClassID[purple]);{Q6:这个是DataBkr单元下的过程,什么作用的?}[/purple]<br> &nbsp; &nbsp;EnableWebTransport(ClassID[blue]);{Q7:这个也是}[/blue]<br> &nbsp;end else<br> &nbsp;begin<br> &nbsp; &nbsp;DisableSocketTransport(ClassID[purple]);{Q8:这个也是DataBkr单元下的,没碰过呀,[/purple]汗。。。}<br> &nbsp; &nbsp;DisableWebTransport(ClassID[blue]);{Q9:这个也是呀。。。不懂}[/blue]<br> &nbsp; &nbsp;inherited UpdateRegistry(Register, ClassID, ProgID);{[purple]Q10:这语句是怎么回事呀?又回到了Register为真时的第一个语句}[/purple] &nbsp;end;<br>end;<br>//连接数据库<br>procedure TMy2005Class.RemoteDataModuleCreate(Sender: TObject);<br>begin<br>ADOConnection1.ConnectionString:=setadoaccess(extractfilepath(paramstr(0))+'tmp.mdb','');<br>end;<br>//数据库连接加1<br>procedure TMy2005Class.ADOConnection1ConnectComplete(Connection: TADOConnection;<br> &nbsp;const Error: Error; var EventStatus: TEventStatus);<br>begin<br>inc(i);<br>form1.Label2.Caption:=inttostr(i);<br>end;<br>//数据库连接减1<br>procedure TMy2005Class.ADOConnection1Disconnect(Connection: TADOConnection;<br> &nbsp;var EventStatus: TEventStatus);<br>begin<br>dec(I);<br>form1.Label2.Caption:=inttostr(i);<br>end;<br>[purple]{Q10:在单元的最后一句话,这个是作用的?他用到的各个参数是什么作用的,从来没有用过,可能整个单元最重要的语句就是句了吧?说说}[/purple]initialization<br> &nbsp;TComponentFactory.Create(ComServer, TMy2005Class,<br> &nbsp; &nbsp;Class_My2005Class, ciMultiInstance, tmApartment);<br>End<br><br>[blue]{Q11:下面这个单元文件,好像是每一个三层例子都有的,至于怎么来的,我不知道,不像是自己写的吧?有的网友说是自动生成的,但是也说得一句带过,过程怎么样没有说明。下面有一句,我在客户端的DCOMConnection1控件设置中看到:CLASS_My2005Class: TGUID = '{96265267-9111-4D49-B194-20260052D669}';他的值是怎么定义的?也是自动生成的吗?有人可以详细说明生成这个接口文件的操作步骤吗?是不是每个接口都是一样的?如果不是,有何区别?}[/blue]unit Project1_TLB;<br><br>// ************************************************************************ //<br>// WARNING &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>// ------- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>// The types declared in this file were generated from data read from a &nbsp; &nbsp; &nbsp; <br>// Type Library. If this type library is explicitly or indirectly (via &nbsp; &nbsp; &nbsp; &nbsp;<br>// another type library referring to this type library) re-imported, or the &nbsp; <br>// 'Refresh' command of the Type Library Editor activated while editing the &nbsp; <br>// Type Library, the contents of this file will be regenerated and all &nbsp; &nbsp; &nbsp; &nbsp;<br>// manual modifications will be lost. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>// ************************************************************************ //<br><br>// PASTLWTR : 1.2<br>// File generated on 2006-8-8 19:23:16 from Type Library described below.<br><br>// ************************************************************************ &nbsp;//<br>// Type Lib: D:/TDDownload/软件/Delphi 2005 ADO 三层数据库演示.asp/rs/server/Project1.tlb (1)<br>// LIBID: {F7F9EC06-89C6-4048-B342-398E74E22267}<br>// LCID: 0<br>// Helpfile: <br>// HelpString: Project1 Library<br>// DepndLst: <br>// &nbsp; (1) v1.0 Midas, (C:/WINDOWS/system32/midas.dll)<br>// &nbsp; (2) v2.0 stdole, (C:/WINDOWS/system32/stdole2.tlb)<br>// ************************************************************************ //<br>{$TYPEDADDRESS OFF} // Unit must be compiled without type-checked pointers. <br>{$WARN SYMBOL_PLATFORM OFF}<br>{$WRITEABLECONST ON}<br>{$VARPROPSETTER ON}<br>interface<br><br>uses Windows, ActiveX, Classes, Graphics, Midas, StdVCL, Variants;<br> &nbsp;<br><br>// *********************************************************************//<br>// GUIDS declared in the TypeLibrary. Following prefixes are used: &nbsp; &nbsp; &nbsp; &nbsp;<br>// &nbsp; Type Libraries &nbsp; &nbsp; : LIBID_xxxx &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>// &nbsp; CoClasses &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: CLASS_xxxx &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>// &nbsp; DISPInterfaces &nbsp; &nbsp; : DIID_xxxx &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>// &nbsp; Non-DISP interfaces: IID_xxxx &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>// *********************************************************************//<br>const<br> &nbsp;// TypeLibrary Major and minor versions<br> &nbsp;Project1MajorVersion = 1;<br> &nbsp;Project1MinorVersion = 0;<br><br> &nbsp;LIBID_Project1: TGUID = '{F7F9EC06-89C6-4048-B342-398E74E22267}';<br><br> &nbsp;IID_IMy2005Class: TGUID = '{B4535BB1-5B6D-438F-8188-B1E368574050}';<br> &nbsp;CLASS_My2005Class: TGUID = '{96265267-9111-4D49-B194-20260052D669}';<br>type<br><br>// *********************************************************************//<br>// Forward declaration of types defined in TypeLibrary &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>// *********************************************************************//<br> &nbsp;IMy2005Class = interface;<br> &nbsp;IMy2005ClassDisp = dispinterface;<br><br>// *********************************************************************//<br>// Declaration of CoClasses defined in Type Library &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>// (NOTE: Here we map each CoClass to its Default Interface) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>// *********************************************************************//<br> &nbsp;My2005Class = IMy2005Class;<br><br><br>// *********************************************************************//<br>// Interface: IMy2005Class<br>// Flags: &nbsp; &nbsp; (4416) Dual OleAutomation Dispatchable<br>// GUID: &nbsp; &nbsp; &nbsp;{B4535BB1-5B6D-438F-8188-B1E368574050}<br>// *********************************************************************//<br> &nbsp;IMy2005Class = interface(IAppServer)<br> &nbsp; &nbsp;['{B4535BB1-5B6D-438F-8188-B1E368574050}']<br> &nbsp;end;<br><br>// *********************************************************************//<br>// DispIntf: &nbsp;IMy2005ClassDisp<br>// Flags: &nbsp; &nbsp; (4416) Dual OleAutomation Dispatchable<br>// GUID: &nbsp; &nbsp; &nbsp;{B4535BB1-5B6D-438F-8188-B1E368574050}<br>// *********************************************************************//<br> &nbsp;IMy2005ClassDisp = dispinterface<br> &nbsp; &nbsp;['{B4535BB1-5B6D-438F-8188-B1E368574050}']<br> &nbsp; &nbsp;function AS_ApplyUpdates(const ProviderName: WideString; Delta: OleVariant; MaxErrors: Integer; <br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out ErrorCount: Integer; var OwnerData: OleVariant): OleVariant; dispid 20000000;<br> &nbsp; &nbsp;function AS_GetRecords(const ProviderName: WideString; Count: Integer; out RecsOut: Integer; <br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Options: Integer; const CommandText: WideString; var Params: OleVariant; <br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var OwnerData: OleVariant): OleVariant; dispid 20000001;<br> &nbsp; &nbsp;function AS_DataRequest(const ProviderName: WideString; Data: OleVariant): OleVariant; dispid 20000002;<br> &nbsp; &nbsp;function AS_GetProviderNames: OleVariant; dispid 20000003;<br> &nbsp; &nbsp;function AS_GetParams(const ProviderName: WideString; var OwnerData: OleVariant): OleVariant; dispid 20000004;<br> &nbsp; &nbsp;function AS_RowRequest(const ProviderName: WideString; Row: OleVariant; RequestType: Integer; <br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var OwnerData: OleVariant): OleVariant; dispid 20000005;<br> &nbsp; &nbsp;procedure AS_Execute(const ProviderName: WideString; const CommandText: WideString; <br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var Params: OleVariant; var OwnerData: OleVariant); dispid 20000006;<br> &nbsp;end;<br><br>// *********************************************************************//<br>// The Class CoMy2005Class provides a Create and CreateRemote method to &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>// create instances of the default interface IMy2005Class exposed by &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>// the CoClass My2005Class. The functions are intended to be used by &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>// clients wishing to automate the CoClass objects exposed by the &nbsp; &nbsp; &nbsp; &nbsp; <br>// server of this typelibrary. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>// *********************************************************************//<br> &nbsp;CoMy2005Class = class<br> &nbsp; &nbsp;class function Create: IMy2005Class;<br> &nbsp; &nbsp;class function CreateRemote(const MachineName: string): IMy2005Class;<br> &nbsp;end;<br><br>implementation<br><br>uses ComObj;<br><br>class function CoMy2005Class.Create: IMy2005Class;<br>begin<br> &nbsp;Result := CreateComObject(CLASS_My2005Class) as IMy2005Class;<br>end;<br><br>class function CoMy2005Class.CreateRemote(const MachineName: string): IMy2005Class;<br>begin<br> &nbsp;Result := CreateRemoteComObject(MachineName, CLASS_My2005Class) as IMy2005Class;<br>end;<br><br>end..<br>.
 
接口的声明文件,在IDE中可以被Delphi用Lib View(大概是这么个东西,好像在Project菜单下)打开,编译时使用,通过导入dll or 自己制作com组建时IDE自动生成
 
楼主问得好呀, 对这三层的结构也是一知半解呀, 特别是自动加入的那些, 期待ing!
 
正在学习.<br>期待ing
 
读书馆可能会有
 
留下联系方法!!
 
to lnming@yeah.net<br> &nbsp;QQ:29938954
 
求书者,感动中,我的书柜中这三本都有,当时还是下了决心才买的,就看了《delphi 5.x ADO/MTS/COM+高级程序设计篇》真正的好书!
 
网上都有的,那三本我都下了。仔细找找吧
 
电子版我下了,但是看得太累,难以安静理解.
 
只有一本了: &lt;&lt;Delphi 5.X 公布式多层应用电子商务篇&gt;&gt; 自取10块[:D]
 
to JonnyZheng:先谢谢你.你在哪里的?我在广东茂名.如果我们不是同一个地方的话,肯定要邮寄了.要加多少钱邮寄,你开个价吧.我总不能让你亏了.
 
运费: http://www.sf-express.com/sfwebapp/price.jsp<br>起点广州,重量不清楚(430页)估计费用 ¥12 左右。<br><br>如果需要发MAIL: Jonnyzheng@163.com 联系。
 
to JonnyZheng:邮件已发.<br><br>其他大伙还有愿意转让的吗?请留言.<br>谢谢
 
我倒是都有,嘿嘿。
 
to jeansonliu:可以转让吗?
 
虽然已经转行不做开发了,可是还是不舍得卖,抱歉!
 
既然不做开发了,留着还有何用呢?转让至少可以让多一个人懂,他日交流至少也多一个人呀.是不是?
 
你也买????不要想了,我以前发了许多分,也没有买到...<br>最后还是看电子版本的.......WWW.DEPHIBOX.COM都有这些书的电子版本的...<br>买?够戗了...<br>后来我出版社报名,也没有收到任何回应..
 
电子版的印刷质量太差,很多地方都看不清楚
 

Similar threads

后退
顶部