//********Udm***********
unit Udm;
interface
uses
SysUtils, Classes, DB, ADODB;
const
strConn = 'Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;'+
'Initial Catalog=Test;Data Source=(local)';
type
TDM = class(TDataModule)
con1: TADOConnection;
ds1: TDataSource;
procedure DataModuleCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
DM: TDM;
implementation
{$R *.dfm}
procedure TDM.DataModuleCreate(Sender: TObject);
begin
DM.con1.ConnectionString := strConn;
DM.con1.LoginPrompt := False;
end;
end.
//***********Uentities*********
unit Uentities;
interface
type
TSex = class
private
FSex: string;
procedure SetSex(value: string);
function GetSex: string;
public
constructor create;
destructor Destory;
published
property Sex: string read GetSex write SetSex;
end;
/// <summary>
/// TMan's entity layer
/// </summary>
TMan = class
private
FName: string;
FSex: TSex;
procedure SetName(value: string);
function GetName: string;
procedure SexSex(value: TSex);
function GetSex: TSex;
public
constructor create;
destructor Destory;
published
property Name: string read GetName write SetName;
property Sex: TSex read GetSex write SexSex;
end;
implementation
{ TSex }
constructor TSex.create;
begin
FSex := '男'; //default
end;
destructor TSex.Destory;
begin
inherited;
end;
//********* ManDAL************
unit Udal;
interface
uses Uentities, Udm, ADODB, DB, Classes;
type
/// <summary>
/// TMan's data access layer
/// </summary>
TManDAL = class
private
adoQry: TADOQuery;
public
//Show
function Show(): TDataSet;
constructor Create;
destructor Destory;
end;
implementation
{ TManDAL }
constructor TManDAL.Create;
begin
inherited;
adoQry := TADOQuery.Create(Nil);
adoQry.Connection := DM.con1;
DM.ds1.DataSet := adoQry;
end;
destructor TManDAL.destory;
begin
if Assigned(adoQry) then
adoQry.Free;
inherited;
end;
/// <summary>
/// show all record
/// </summary>
function TManDAL.Show(): TDataSet;
begin
if Assigned(adoQry) then
begin
with adoQry do
begin
Close;
SQL.Clear;
SQL.Add('select * from man');
ExecSQL;
end;
end;
if not adoQry.IsEmpty then
Result := adoQry
else
Result := nil;
end;
end.
//**********IBLL*******
unit UIBLL;
interface
uses Uentities, DB;
type
IBLL = interface(IInterface)
['{BE0C11D9-BB6E-4943-B9BF-9669828D69F0}']
function Del(value: TMan): Boolean;
function Insert(value: TMan): Boolean;
function Show(): TDataSet;
end;
implementation
end.
//**********ManBLL************
unit UBLL;
interface
uses Udal, Uentities, UIBLL, DB, Classes;
type
/// <summary>
/// TMan's bussiness logic layer
/// </summary>
TManBLL = class(TInterfacedObject, IBLL)
private
FManDAL: TManDAL;
//show all record
function Show(): TDataSet;
public
constructor create;
destructor Destory;
end;
implementation
{ TManBLL }
constructor TManBLL.create;
begin
inherited;
FManDAL := TManDAL.Create; //这行使调用出错
end;
destructor TManBLL.Destory;
begin
if Assigned(FManDAL) then
FManDAL := nil;
inherited;
end;
function TManBLL.Show(): TDataSet;
begin
Result := FManDAL.Show();
end;
end.
//********Library Unit*********
library Layer;
{ 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,
ShareMem,
Uentities in 'Uentities.pas',
Udm in 'Udm.pas',
UBLL in 'UBLL.pas',
UIBLL in 'UIBLL.pas';
{$R *.res}
function ManBLL: IBLL; stdcall;
begin
Result := TManBLL.create;
end;
exports
ManBLL;
begin
end.
//**********实现调用dll*****
unit Umain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, DBGrids, ADODB, StdCtrls, DB, UIBLL;
type
TForm1 = class(TForm)
dbgrd1: TDBGrid;
btn1: TButton;
btnAdd: TButton;
btnDel: TButton;
edtName: TEdit;
lbl1: TLabel;
lbl2: TLabel;
cbbSex: TComboBox;
ds1: TDataSource;
procedure dbgrd1CellClick(Column: TColumn);
procedure btnAddClick(Sender: TObject);
procedure btnDelClick(Sender: TObject);
procedure dbgrd1DblClick(Sender: TObject);
procedure btn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
function ManBLL: IBLL; stdcall; external 'Layer.dll';
implementation
uses Uentities;
{$R *.dfm}
procedure TForm1.btn1Click(Sender: TObject);
var
showMan: IBLL;
begin
showMan := ManBLL;
ds1.DataSet := showMan.Show;
showMan := nil;
end;
end.
代码已经全部贴出,望各位帮找下错误