在开发D5+SQL SERVER的大型项目中,如何实现数据连接的共享!(200分)

  • 主题发起人 主题发起人 WYHTH
  • 开始时间 开始时间
W

WYHTH

Unregistered / Unconfirmed
GUEST, unregistred user!
在开发D5+SQL SERVER用ADO连接的大型项目中,将其化分为几个模块,各模块编译为独立的EXE
,用主控模块来连接SQL SERVER数据库,并调用各模块相应的EXE 问:如何实现各模块能共享主
控模块的数据连接.(每次重新连接速度太慢!)
 
主控模块完成联接,并给其它模块提供活动DATASET.
 
把其它模块作成dll不好吗?可以共享连接了吗不是?
和外部进程共享连接好像比较困难哦,除非使用com了,那还不如作dll省事呢
 
To iforever
能不能具体一点!

TO CJ
每一个EXE都有数据模块,只是想共享主控模块的ADO连接!应该怎么办?
 
独立进程看来不行
 
>每一个EXE都有数据模块,只是想共享主控模块的ADO连接
这跟cj的话并不矛盾啊
有自己的数据模板就不能做成dll了?
如果你一定要用exe,那只能com了
 
谢谢理解了我的意思。
我还是推荐用dll,修改不用多少时间阿
我知道你要共享的是adoconnection
 
DLL的写法:
library pdll;

{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }

uses
SysUtils,
Classes,
adodb,
db,
windows,
forms,
Unit1 in 'Unit1.pas' {Form1},
Unit2 in 'Unit2.pas' {Form2};

{$R *.RES}


exports
run;

begin
END;

//DLL中FROM1的代码
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Db, ADODB, Grids, DBGrids;

type
TForm1 = class(TForm)
DBGrid1: TDBGrid;
q1: TADOQuery;
ds: TDataSource;
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
end;
var form1:tform1;

function run(a:thandle;con:TADOConnection):boolean;stdcall;
implementation

uses Unit2;

{$R *.DFM}

function run(a:thandle;con:TADOConnection):boolean;stdcall;
begin
application.Handle:=a;
form1:=tform1.Create(application);
try
try
form1.q1.Connection:=con;
form1.showmodal;
result:=true;
except
result:=false;
end;
finally
form1.free;
end;
end;



procedure TForm1.Button1Click(Sender: TObject);
begin
q1.open;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
close;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
application.createform(tform2,form2);
try
form2.showmodal;
finally
form2.free;
end;

end;

end.
end.

//DLL中FROM2的代码
unit Unit2;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Buttons, Grids, DBGrids;

type
TForm2 = class(TForm)
BitBtn1: TBitBtn;
dg1: TDBGrid;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form2: TForm2;

implementation

uses Unit1;

{$R *.DFM}

procedure TForm2.Button1Click(Sender: TObject);
begin
dg1.DataSource:=form1.dd;
caption:=inttostr(form1.q1.RecordCount)
end;

end.


主控程序:
unit main;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Db, ADODB, Grids, DBGrids;

type
{ First, define a procedural data type, this should reflect the
procedure that is exported from the DLL. }
TRUN =function (a:thandle;con:TADOConnection):boolean;stdcall;

{ Create a new exception class to refect a failed DLL load }
EDLLLoadError = class(Exception);

TForm1 = class(TForm)
Button1: TButton;
adoc: TADOConnection;
q: TADOQuery;
ds: TDataSource;
DBGrid1: TDBGrid;
procedure FormShow(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormShow(Sender: TObject);
begin
q.open;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
LibHandle : THandle;
RUN: TRUN;
begin

{ Attempt to load the DLL }
LibHandle := LoadLibrary('PDLL.DLL');
try
{ If the load failed, LibHandle will be zero.
If this occurs, raise an exception. }
if LibHandle = 0 then
raise EDLLLoadError.Create('Unable to Load DLL');
{ If the code makes it here, the DLL loaded successfully, now obtain
the link to the DLL's exported function so that it can be called. }
@RUN := GetProcAddress(LibHandle, 'run');
{ If the function is imported successfully, then set lblDate.Caption to reflect
the returned date from the function. Otherwise, show the return raise
an exception. }
if not (@RUN = nil) then
RUN(Application.Handle, ADOC)
else
RaiseLastWin32Error;
finally
FreeLibrary(LibHandle); // Unload the DLL.
end;
end;

end.
 
终于又联上来了.

不要COM,其他模块也无需做成DLL.

主控模块在加载时,加在一个DLL, 其它模块都从这DLL里取的DATASET
 
做成三层不就行了?
  应用SEERVER可以看情况放在数据库机器上或分别地放在客个客户端
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
2K
DelphiTeacher的专栏
D
后退
顶部