在你的硬碟上建立一代码要放置的目录,如:
d:/MulitSys
d:/MulitSys/Server
d:/MulitSys/Client
1。建立Server端
(1)打开Delphi7,File -->New --> Application -->变更Form1的名称为:Name:='Serverfrm';
保存Serverfrm到d:/Mulitsys/Server/下,
.pass文件命名为:pu_server.pas;
Project命名为:pf_server.dpr
变更Serverfrm的WindowState的值为:wsMinimized
(2)View -->Project Manager -->选取项目pf_server.exe,然后在工具栏上选择New Items按钮,弹出New Items窗口,选择Mulitier标签,再选择Remove Data Module,建立服务器端运程数据模块-->点"OK",进入到"Remove Data Module Wizard"向导。
输入:Class Name : Mulit_ServerData-->确定
(3)建立数据库链接
在Mulit_ServerData中放一个ADOConnection组件(在ADO标签中),将ADOConnection1的KeepConnection的属性置为True,LoginPromt属性置为 False;
保存以上操作,将Remove Data Module的class命名为:pu_Mulit_ServerData.pas
(4)通过ini文件读取数据库链接
首先在pu_Mulit_ServerData.pas的Uses中导入文件操作类,如:
Uses Inifiles;
再声明私有变量myInifile为TInifile类型。如下:
private
{ Private declarations }
myInifile:TInifile;
编写TMulit_ServerData.RemoteDataModuleCreate的事件如下:
procedure TKMServerData.RemoteDataModuleCreate(Sender: TObject);
var
filename,
ConnectionStr:string;
begin
filename:=ExtractFilePath(paramstr(0))+'Serverini.ini';
myInifile:=TInifile.Create(filename);
ConnectionStr:=myInifile.ReadString('ServerConfig','ConnectionString','');
ADOConnection1.ConnectionString:=ConnectionStr;
ADOConnection1.Connected:=true;
ADOConnection1.KeepConnection:=true;
ADOConnection1.LoginPrompt:=False;
end;
(5)Server端的Ini文件命名为:d:/MulitSys/Server/server.ini内容如下:
[ServerConfig]
ConnectionString=Provider=SQLOLEDB.1;Password=;Persist Security Info=True;User ID=sa;Initial Catalog=KM_ERP;Data Source=(local)
通过以上架构,基本上就选建立起Server层了。
然后按保存,来保存整个项目文件D:/MulitSys/MulitSys.bpg,下次进入本项目只需要直接点击MulitSys.bpg就可以打开了。
2.搭建Client端框架
(1)View à Project Manager à New (Add New Project) à选择New中的Application,并且为form1命名
FormStyle:=fsMDIForm;
WindowState:= wsMaximized;
Position:= poScreenCenter;
(2)保存项目
按保存按钮,将class文件保存为pu_Main.pas,项目文件,也就是要生成的exe文件保存为MulitSys.dpr。
(3)建立Client端数据模块
将MulitSys项目置为活动项目,然后File àNew à DataModule à为DataModule命名为MulitSys_ClientData
(4)建立和服务器端的链接
在DataModule上放置DCOMConnection,并设置它的属性,如下:
ServerName:=pf_server.Mulit_ServerData;
Connectioned = true;
LoginPromt =false;
通过以上方法,Server端和Client端(中间层)都建立起来了,并且可以运行了。
链接示例:
例1:对部门的数据进行操作,数据库略。
在SererDataModule中建立TADODataSet组件,属性如下:
Name :=DeptADS;
Connection:=ADOConnection1
CommandText: = “select * from tblDept”;
再放置TdataSetProvider组件,并设置其属性如下:
Name:=DeptDSP;
DataSet:=DeptADS;
在ClientDataModule中放置TclientDataSet组件,并设置其属性如下:
Name:= DeptCDS;
RemoveServer:=DCOMConnection1;
ProViderName:= DeptDSP;
这样,整个中间层已建立起来了
3.建立表示层业务
选择MulitSys为活动项目,在刚才建立起的Mainfrm上放置菜单,根据需要设置菜单项,略。
建立Dept 的表示层,并进行简单数据操作。
File à New à Form
对Form命名,放置DBGrid组件到Form上,并放置SpeedButton按钮到Form上,分别命名为AddBtn,UpdateBtn,DelBtn,SearchBtn,SaveBtn,CancelBtn,PrintBtn,ExitBtn,等。
再从Fileà Use Unit 中,选择ClientDataModule,将它use到deptfrm中。
再放置一TdataSource组件到Form上,设置DataSet的属性如下:
在选择DataSource的DataSet属性前需要选打开ClientData,否则DataSource的DataSet下拉框中无值。
选择,DeptCDS为DataSet属性值。
再将DBGrid1的DataSource值为DataSource1。前面那些数据库的操作按钮见下面代码。
unit pu_Dept;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, Grids, DBGrids, ExtCtrls, Buttons, StdCtrls, Mask, DBCtrls,
ComCtrls;
type
TDeptfrm = class(TForm)
Panel1: TPanel;
Panel2: TPanel;
DBGrid1: TDBGrid;
Panel3: TPanel;
DataSource1: TDataSource;
BackBtn: TSpeedButton;
NextBtn: TSpeedButton;
FindBtn: TSpeedButton;
PrintBtn: TSpeedButton;
AddBtn: TSpeedButton;
EditBtn: TSpeedButton;
DeleteBtn: TSpeedButton;
SaveBtn: TSpeedButton;
CancelBtn: TSpeedButton;
ExitBtn: TSpeedButton;
PageControl1: TPageControl;
TabSheet1: TTabSheet;
Label1: TLabel;
Label2: TLabel;
DBEdit1: TDBEdit;
DBEdit2: TDBEdit;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormCreate(Sender: TObject);
procedure Btn;
procedure UnBtn;
procedure BackBtnClick(Sender: TObject);
procedure NextBtnClick(Sender: TObject);
procedure AddBtnClick(Sender: TObject);
procedure EditBtnClick(Sender: TObject);
procedure DeleteBtnClick(Sender: TObject);
procedure SaveBtnClick(Sender: TObject);
procedure CancelBtnClick(Sender: TObject);
procedure ExitBtnClick(Sender: TObject);
private
{ Private declarations }
procedure OnMouseWheel(Var Msg :TMsg;var Handled:Boolean);
public
{ Public declarations }
end;
var
Deptfrm: TDeptfrm;
implementation
uses pu_ClientData, pf_pubfunction;
{$R *.dfm}
//正常
procedure TDeptfrm.Btn;
begin
SaveBtn.Enabled:=false;
CancelBtn.Enabled:=false;
panel3.Visible:=false;
BackBtn.Enabled:=true;
NextBtn.Enabled:=true;
FindBtn.Enabled:=true;
PrintBtn.Enabled:=true;
{AddBtn.Enabled:=pf_pubfunction.p_checkgrouppower(tform(panel1.Owner).Name,'AddBit');
EditBtn.Enabled:=pf_pubfunction.p_checkgrouppower(tform(panel1.Owner).Name,'EditBit');
DeleteBtn.Enabled:=pf_pubfunction.p_checkgrouppower(tform(panel1.Owner).Name,'DelBit');
}
AddBtn.Enabled:=true;
EditBtn.Enabled:=true;
DeleteBtn.Enabled:=true;
ExitBtn.Enabled:=true;
end;
//解锁
procedure TDeptfrm.UnBtn;
begin
SaveBtn.Enabled:=true;
CancelBtn.Enabled:=true;
Panel3.Visible:=true;
BackBtn.Enabled:=false;
NextBtn.Enabled:=false;
FindBtn.Enabled:=false;
PrintBtn.Enabled:=false;
AddBtn.Enabled:=false;
EditBtn.Enabled:=false;
DeleteBtn.Enabled:=false;
ExitBtn.Enabled:=false;
end;
Procedure TDeptfrm.OnMouseWheel(Var Msg :TMsg;var Handled:Boolean);
begin
if Msg.message = WM_MouseWheel then
begin
if Msg.wParam > 0 then
begin
if DBGrid1.Focused then
SendMessage(DBGrid1.Handle,WM_VSCROLL,SB_PAGEUP,0);
end
else
begin
if DBGrid1.Focused then
SendMessage(DBGrid1.Handle,WM_VSCROLL,SB_PAGEDOWN,0);
end;
Handled:= True;
end;
end;
procedure TDeptfrm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
action:=cafree;
end;
procedure TDeptfrm.FormCreate(Sender: TObject);
begin
ClientData.DeptCDS.Active:=true;
Btn;
Application.OnMessage:=OnMouseWheel;
end;
procedure TDeptfrm.BackBtnClick(Sender: TObject);
begin
ClientData.DeptCDS.Prior;
end;
procedure TDeptfrm.NextBtnClick(Sender: TObject);
begin
ClientData.DeptCDS.Next;
end;
procedure TDeptfrm.AddBtnClick(Sender: TObject);
begin
UnBtn;
ClientData.DeptCDS.Insert;
ClientData.DeptCDS.FieldValues['DeptNO']:=pf_pubfunction.p_getmaxno('tblDept','DeptNO',3);
DBEdit2.SetFocus;
end;
procedure TDeptfrm.EditBtnClick(Sender: TObject);
begin
UnBtn;
ClientData.DeptCDS.Edit;
end;
procedure TDeptfrm.DeleteBtnClick(Sender: TObject);
begin
//if MessageDlg('确实要删除吗?',mtConfirmation,[mbYes, mbNo],0)=mrYes then
if MessageDlg('Are you confirm Delete?',mtConfirmation,[mbYes,mbNo],0)=mrYes then
begin
ClientData.DeptCDS.Delete;
ClientData.DeptCDS.ApplyUpdates(-1);
end;
end;
procedure TDeptfrm.SaveBtnClick(Sender: TObject);
begin
Btn;
ClientData.DeptCDS.ApplyUpdates(0);
end;
procedure TDeptfrm.CancelBtnClick(Sender: TObject);
begin
Btn;
ClientData.DeptCDS.Cancel;
end;
procedure TDeptfrm.ExitBtnClick(Sender: TObject);
begin
close;
end;
end.
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/zssyq/archive/2006/06/24/828964.aspx