Delphi three-tier architecture database operation strategy

Source: Internet
Author: User

Create a directory for code placement on your hard drive, such:
D:/mulitsys
D:/mulitsys/Server
D:/mulitsys/client
1. Create a server
(1) Open Delphi7, file --> New --> Application --> change the form1 name to: Name: = 'serverfrm ';
Save serverfrm to D:/mulitsys/Server,
The. Pass file is named pu_server.pas;
Project name: pf_server.dpr
Change serverfrm's windowstate value to: wsminimized
(2) View --> project manager --> select the project pf_server.exe, and select the new items button on the toolbar. The new items window is displayed. Select the mulitier tag, and then select Remove data module, create a data module on the server --> click "OK" to go to the "remove data module wizard" Wizard.
Input: Class Name: mulit_serverdata --> OK
(3) create a database link
Put an adoconnection component in mulit_serverdata (in the ADO tag), set the keepconnection attribute of adoconnection1 to true, and the loginpromt attribute to false;
Save the preceding operation and name the remove data module class pu_mulit_serverdata.pas.
(4) read the database link through the INI File
First, import the file operation class in uses of pu_mulit_serverdata.pas, for example:
Uses inifiles;
Then declare the private variable myinifile as the Tinifile type. As follows:
Private
{Private Declarations}
Myinifile: Tinifile;
The event for compiling tmulit_serverdata.remotedatamodulecreate 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) The INI file on the server 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, the server layer is basically set up.
Then press save to save the entire project file D:/mulitsys. BPG. Next time you enter the project, you only need to click mulitsys. BPG to open it.
2. Build a client framework
(1) view à Project Manager à New (add new project) à select Application in new and name it form1
Formstyle: = fsmdiform;
Windowstate: = wsmaximized;
Position: = poscreencenter;
(2) Save the project
Press the Save button to save the class file as pu_main.pas, the project file, that is, the EXE file to be generated, as mulitsys. DPR.
(3) Establish the client data module
Set the mulitsys project to an active project, and name file à New à datamodule as mulitsys_clientdata.
(4) establish a connection with the server
Place dcomconnection on datamodule and set its attributes as follows:
Servername: = pf_server.mulit_serverdata;
Connectioned = true;
Loginpromt = false;
 
 
Through the above method, both the server and client (intermediate layer) are established and can run.
 
Link example:
Example 1: perform operations on the data of a department. The database is omitted.
Create the TADODataSet component in sererdatamodule with the following attributes:
Name: = deptads;
Connection: = adoconnection1
Commandtext: = "select * From tbldept ";
Place the tdatasetprovider component and set its attributes as follows:
Name: = deptdsp;
Dataset: = deptads;
Place the tclientdataset component in clientdatamodule and set its attributes as follows:
Name: = deptcds;
Removeserver: = dcomconnection1;
Providername: = deptdsp;
 
In this way, the entire middle layer has been established
 
3. Create a presentation layer business
Select mulitsys as the active project, place the menu on the mainfrm created just now, and set the menu item as needed, omitted.
Create a dept presentation layer and perform simple data operations.
File à New à form
Name the form, place the DBGrid component on the form, and put the SpeedButton on the form, respectively named addbtn, updatebtn, delbtn, searchbtn, savebtn, cancelbtn, printbtn, exitbtn, and so on.
Select clientdatamodule from file à use unit and use it to deptfrm.
Place a tdatasource component on form and set the properties of dataset as follows:
You must enable clientdata before selecting the dataset attribute of datasource. Otherwise, the dataset drop-down box of datasource has no value.
Select deptcds as the dataset attribute value.
Then, set the datasource value of dbgrid1 to performance1. For the operation buttons of the databases above, 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;
Performance1: 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}
 
// Normal
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;
 
// 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 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 ('tbldep', 'deptno', 3 );
Dbedit2.setfocus;
End;
 
Procedure tdeptfrm. editbtnclick (Sender: tobject );
Begin
Unbtn;
Clientdata. deptcds. Edit;
End;
 
Procedure tdeptfrm. deletebtnclick (Sender: tobject );
Begin
// If messagedlg ('Are you sure you want to delete it? ', 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.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/zssyq/archive/2006/06/24/828964.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.