M
MingWord
Unregistered / Unconfirmed
GUEST, unregistred user!
先作个声明,本人对三层还未入门,所以大家回答的时候,请尽量想像一下你们当时的情况,用通俗的语句表达出来.<br>这个问题本来是想问三层数据如何连接的,但是问问来问去,还是一样没有答案.一来是自己的问话水平差,二来高手也不屑来回答案笨鸟的这类问题,所以我还是用一个我在网上找到的例子,你们帮我解释一下,或许这样容易理解<br>下面是两个单元文件.<br>unit Unit2;<br>{$WARN SYMBOL_PLATFORM OFF}<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Classes, ComServ, ComObj, VCLCom, DataBkr,<br> DBClient, Project1_TLB{[blue]这个就是那个LIB文件,我一直不明白他是如何来的[/blue]}, DB, ADODB, StdVcl, Provider;<br><br>type<br> 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> ADOConnection1: TADOConnection;<br> ADOQuery1: TADOQuery;<br> DataSetProvider1: TDataSetProvider;<br> procedure ADOConnection1Disconnect(Connection: TADOConnection;<br> var EventStatus: TEventStatus);{这个可以看得明白}<br> procedure ADOConnection1ConnectComplete(Connection: TADOConnection;<br> const Error: Error; var EventStatus: TEventStatus);{这个也可以看得明白}<br> procedure RemoteDataModuleCreate(Sender: TObject);{这个看不明白}<br> private<br> { Private declarations }<br> protected<br> class procedure UpdateRegistry(Register: Boolean; const ClassID, ProgID: string); override;<br>[blue]{Q2:这个class又是作什么用的?}[/blue]<br> public<br> { Public declarations }<br> end;<br>Const<br> SConnectionString = 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;'<br> +'Jet OLEDBatabase 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> if Register then<br> begin<br> inherited UpdateRegistry(Register, ClassID, ProgID[blue]);{Q5:这个是继承怎么回事,说说}[/blue] EnableSocketTransport(ClassID[purple]);{Q6:这个是DataBkr单元下的过程,什么作用的?}[/purple]<br> EnableWebTransport(ClassID[blue]);{Q7:这个也是}[/blue]<br> end else<br> begin<br> DisableSocketTransport(ClassID[purple]);{Q8:这个也是DataBkr单元下的,没碰过呀,[/purple]汗。。。}<br> DisableWebTransport(ClassID[blue]);{Q9:这个也是呀。。。不懂}[/blue]<br> inherited UpdateRegistry(Register, ClassID, ProgID);{[purple]Q10:这语句是怎么回事呀?又回到了Register为真时的第一个语句}[/purple] 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> 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> var EventStatus: TEventStatus);<br>begin<br>dec(I);<br>form1.Label2.Caption:=inttostr(i);<br>end;<br>[purple]{Q10:在单元的最后一句话,这个是作用的?他用到的各个参数是什么作用的,从来没有用过,可能整个单元最重要的语句就是句了吧?说说}[/purple]initialization<br> TComponentFactory.Create(ComServer, TMy2005Class,<br> 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 <br>// ------- <br>// The types declared in this file were generated from data read from a <br>// Type Library. If this type library is explicitly or indirectly (via <br>// another type library referring to this type library) re-imported, or the <br>// 'Refresh' command of the Type Library Editor activated while editing the <br>// Type Library, the contents of this file will be regenerated and all <br>// manual modifications will be lost. <br>// ************************************************************************ //<br><br>// PASTLWTR : 1.2<br>// File generated on 2006-8-8 19:23:16 from Type Library described below.<br><br>// ************************************************************************ //<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>// (1) v1.0 Midas, (C:/WINDOWS/system32/midas.dll)<br>// (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> <br><br>// *********************************************************************//<br>// GUIDS declared in the TypeLibrary. Following prefixes are used: <br>// Type Libraries : LIBID_xxxx <br>// CoClasses : CLASS_xxxx <br>// DISPInterfaces : DIID_xxxx <br>// Non-DISP interfaces: IID_xxxx <br>// *********************************************************************//<br>const<br> // TypeLibrary Major and minor versions<br> Project1MajorVersion = 1;<br> Project1MinorVersion = 0;<br><br> LIBID_Project1: TGUID = '{F7F9EC06-89C6-4048-B342-398E74E22267}';<br><br> IID_IMy2005Class: TGUID = '{B4535BB1-5B6D-438F-8188-B1E368574050}';<br> CLASS_My2005Class: TGUID = '{96265267-9111-4D49-B194-20260052D669}';<br>type<br><br>// *********************************************************************//<br>// Forward declaration of types defined in TypeLibrary <br>// *********************************************************************//<br> IMy2005Class = interface;<br> IMy2005ClassDisp = dispinterface;<br><br>// *********************************************************************//<br>// Declaration of CoClasses defined in Type Library <br>// (NOTE: Here we map each CoClass to its Default Interface) <br>// *********************************************************************//<br> My2005Class = IMy2005Class;<br><br><br>// *********************************************************************//<br>// Interface: IMy2005Class<br>// Flags: (4416) Dual OleAutomation Dispatchable<br>// GUID: {B4535BB1-5B6D-438F-8188-B1E368574050}<br>// *********************************************************************//<br> IMy2005Class = interface(IAppServer)<br> ['{B4535BB1-5B6D-438F-8188-B1E368574050}']<br> end;<br><br>// *********************************************************************//<br>// DispIntf: IMy2005ClassDisp<br>// Flags: (4416) Dual OleAutomation Dispatchable<br>// GUID: {B4535BB1-5B6D-438F-8188-B1E368574050}<br>// *********************************************************************//<br> IMy2005ClassDisp = dispinterface<br> ['{B4535BB1-5B6D-438F-8188-B1E368574050}']<br> function AS_ApplyUpdates(const ProviderName: WideString; Delta: OleVariant; MaxErrors: Integer; <br> out ErrorCount: Integer; var OwnerData: OleVariant): OleVariant; dispid 20000000;<br> function AS_GetRecords(const ProviderName: WideString; Count: Integer; out RecsOut: Integer; <br> Options: Integer; const CommandText: WideString; var Params: OleVariant; <br> var OwnerData: OleVariant): OleVariant; dispid 20000001;<br> function AS_DataRequest(const ProviderName: WideString; Data: OleVariant): OleVariant; dispid 20000002;<br> function AS_GetProviderNames: OleVariant; dispid 20000003;<br> function AS_GetParams(const ProviderName: WideString; var OwnerData: OleVariant): OleVariant; dispid 20000004;<br> function AS_RowRequest(const ProviderName: WideString; Row: OleVariant; RequestType: Integer; <br> var OwnerData: OleVariant): OleVariant; dispid 20000005;<br> procedure AS_Execute(const ProviderName: WideString; const CommandText: WideString; <br> var Params: OleVariant; var OwnerData: OleVariant); dispid 20000006;<br> end;<br><br>// *********************************************************************//<br>// The Class CoMy2005Class provides a Create and CreateRemote method to <br>// create instances of the default interface IMy2005Class exposed by <br>// the CoClass My2005Class. The functions are intended to be used by <br>// clients wishing to automate the CoClass objects exposed by the <br>// server of this typelibrary. <br>// *********************************************************************//<br> CoMy2005Class = class<br> class function Create: IMy2005Class;<br> class function CreateRemote(const MachineName: string): IMy2005Class;<br> end;<br><br>implementation<br><br>uses ComObj;<br><br>class function CoMy2005Class.Create: IMy2005Class;<br>begin<br> Result := CreateComObject(CLASS_My2005Class) as IMy2005Class;<br>end;<br><br>class function CoMy2005Class.CreateRemote(const MachineName: string): IMy2005Class;<br>begin<br> Result := CreateRemoteComObject(MachineName, CLASS_My2005Class) as IMy2005Class;<br>end;<br><br>end..<br>.