Delphi three-layer architecture database operation Raiders

xiaoxiao2021-04-09  302

Create a code to place on your hard drive, such as:

D: / MULITSYS

D: / MULITSYS / SERVER

D: / MULITSYS / CLIENT

1

. set up

Server

end

(1)

turn on

Delphi7

,

File -> new -> Application ->

change

Form1

The name is:

Name: = 'ServerFRM';

save

ServerFRM

Until

D: / MULITSYS / SERVER /

under,

.pass

The file is named:

PU_SERVER.PAS

;

PROJECT

named:

Pf_server.dpr

change

ServerFRM

of

WindowState

The value is:

WSMINIMIZED

(2) View -> Project Manager ->

Selection item

PF_SERVER.EXE

, Then select on the toolbar

NEW ITEMS

Button, pop-up

NEW ITEMS

Window, choose

Mulitier

Label, choose

Remove Data Module

Establish a server-side procedure data module

->

point

"Ok"

,Enter

"Remove Data Module Wizard"

Wizard.

enter:

Class name: MULIT_SERVERDATA ->

determine

(3)

Create a database link

in

MULIT_SERVERDATA

In one way

Adoconnection

Assembly

(

in

ADO

Tag

)

,will

Adoconnection1

of

KeepConnection

Attribute

True, Loginpromt

Attribute is set

False;

Save the above operation, will

Remove Data Module

of

Class

named:

PU_MULIT_SERVERDATA.PAS

(4)

by

ini

Document read database link

First

PU_MULIT_SERVERDATA.PAS

of

Uses

Import file operation classes, such as:

Uses inIfiles;

Reproduction private variable

MyiniFile

for

TiniFile

Types of. as follows:

Private {private declarations} myinifile: tinifile;

write

Tmulit_serverData.RemoteDataModulecreate

The event is as follows:

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

End

Ini

The file is named:

D: /mulitsys/server/server.ini

The content is as follows:

[ServerConfig] Connectionstring = provider = SQLOLEDB.1; Password =; persist security info = true; user ID = sa; initial catalog = km_erp; data source = (local)

Through the above architecture, basically select the establishment

Server

Floor.

Then press Save to save the entire project file

D: /mulitsys/mulitsys.bpg

The next time you enter this project only needs to click directly.

Mulitsys.bpg

You can open it.

2.

Build

Client

End frame

(1) View

à

Project Manager

à

New (Add New Project)

à

select

New

middle

APPLICATION,

And

Form1

name

Formstyle: = fsmdiform;

WindowState: =

WSMAXIMIZED;

Position: = poscreencenter;

(2) Save the project

Press the Save button to save the class file as a PU_MAIN.PAS, the project file, that is, the EXE file to be generated is saved as MULITSYS.DPR.

(3) Establish a client data module

Set the Mulitsys item as an active item, then File

ànew

à Datamodule

à Name Mulitsys_ClientData for Datamodule

(4) Connecting and server-side links

Place DCOMCONNECTION on DataModule and set its properties as follows:

Servername: = pf_server.mulit_serverdata;

CONNECTIONED = TRUE;

Loginpromt = false;

By the above method, the Server end and the Client end (intermediate layer) are established and can be run.

Link example:

Example 1: Operation of the data of the department, the database is logged.

Create a tadodataset component in SereDataModule, the properties are as follows:

Name: = deptads;

Connection: = adoconnection1

CommandText: = "Select * from tbldept";

Place the TDataSetProvider component and set its properties as follows: name: = deptdsp;

DataSet: = deptads;

Place the TclientDataSet component in ClientDataModule and set its properties as follows:

Name: = deptcds;

REMOVEServer: = DCOMCONNECTION1;

ProviderName: = deptdsp;

In this way, the entire intermediate layer has been established.

3. Establish a representation layer business

Select MULITSYS to place the menu on the MAINFRM that has just been established, and set the menu item as needed.

Establish a representation of DEPT and make simple data operations.

File

à new

à form

Named the Form, place the DBGRID component to Form, and place the SpeedButton button to Form, named AddBTN, UpdateBTN, Delbtn, Searchbtn, Savebtn, Cancelbtn, PrintBTN, EXITBTN, and so on.

From File

à Un UN, select ClientDataModule, use it to DEPTFRM.

Place a TDataSource component to FORM, set the properties of the DataSet as follows:

You need to select Open ClientData before selecting DataSource's DataSet attribute, otherwise no value in the DataSource's DataSet drop-down box.

Select, deptcds is the DataSet property value.

The DBGRID1's DataSource value will be DataSource1. The operation button of those databases appears to see the following code.

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: Tclosection);

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 HANDED: BOOLEAN);

public

{Public declarations}

END;

VAR

DEPTFRM: TDEPTFRM;

IMPLEMENTATION

Uses Pu_ClientData, PF_PUBFunction;

{$ R * .dfm}

//normal

Procedure tdeptfrm.btn;

Begin

Savebtn.enabled: = false;

Cancelbtn.enabled: = false;

Panel3.visible: = false;

BACKBTN.ENABED: = 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;

// Unlock

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 HANDED: BOOLEAN);

Begin

if msg.Message = WM_MOUSEWHEEL THEN

Begin

IF msg.wpaam> 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: Tclosection);

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 ('Is it true to delete?', MTConfirmation, [mbyes, mbno], 0) = mryes dam

IF Messagedlg ('Are you confirm delete?, MTConfirmation, [mbyes, mbno], 0) = mryes the

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.

Through the above steps, it is basically established a complete three-layer architecture system. As for the release of the program, it is another. Are you successful?

转载请注明原文地址:https://www.9cbs.com/read-133095.html

New Post(0)