Multi-Tier structural program development basic tutorial (2)

zhaozj2021-02-16  61

Multi-Tier architecture program development based tutorial (II.1) Author: z33

This section tells the multi-storey transaction, some places may be more difficult to understand, even don't understand, this is nothing, just do more, you will understand more, learn Delphi's things can't do it. Once, it will, the more you have to do more, you can remember.

First, what is transaction processing

As far as I understand (through Wang Xiaomin's students, I finally have some understanding of the transaction, but I don't know if this understanding is accurate) it is a plurality of multi-related data sets on the client on the client. There is a contact operation in each other, and one of these operations fails when these related operations are executed, then all other operations are canceled, and the data set is rolled back to the state before the operation. So these operations should be handled as a transaction, and it is necessary to roll back. For example, the primary slave structure is the relevant data set. We must remove the primary table data must first remove the relevant records of the table, so it is necessary to use the transaction for their operations.

Second, 1. Built a new project and add the Remote Data Module, the name of the Remote Data Module plays a meaningful and prominent. Plat up the three controls of DataSetProvider, Table, Database. Its properties were set: Database1.AliasName: = DBDEMOS; Database1.DatabaseName: = test; Database1.HandleShared: = true; // Client can make a lot of shared database connections Database1.Translsolation: = tiDirtyRead; // Paradox support because only DirtyReadDatabase1 .Loginprompt: = false; // Do not logindatabase1.connected: = true;

Table1.DatabaseName: = TEST; TABLE1.TABLENAME: = Customer.db; table1.active: = true; datasetProvider1.dataset: = Table1; Figure:

2. Select the Type Library item in the View menu to open the Type Library editing window. On the toolbar of the point window, the ammonity of a green arrow, the server object bar will get a node, you can enter a name, enter ApplyDates, add an ApplyDates method to the server. Figure:

Among them, the id setting in the Attributes column is 1, and the name in the Parameters column fills the custom, Type selects VARIANT, then click the hot ammonium button above, you can turn this window. Figure:

Here, in the Parameters (parameter), the server is set to an interface (interface) for the server, used to receive a transaction from the client, where CustVar is the received variable name, and Variant is a variable type. The third is that IN, which is the action of the server, we are just in (receive variables), not OUT (return variable).

3. Now to write the code of the server: find the implementation of the method of the method that just defined in the code area, this part is this part: procedure tads.applydates (Custvar: Olevariant); BeGund;

The code is as follows: -------------------------- procedure tads.applydates (custom: olevariant); var errcount: integer; // Define an error The integer variable of the number. Begin Database1.startTransAction; // Database Starts Transaction. Try // This is an abnormally handled, corresponding to the following Except, if there is an exception, execute the instruction below the Except. If NOT VARISNULL () THEN / / VARISNULL () is a function if you arguate to empty. Begin // The application operation is operated by the Applyupadtes method of DataSetProvider. Custvar: = datasetProvider1.applyupdates (CustVar, 0, Errcount); // If the number of error is greater than 0, there is an error, interrupt the transaction. IF errcount> 0 dam; database1.commit; // Submit transaction update. Except Database1.rollback; // The error will be rolled back. End; end; ------------------------- This setting is completed. Today, because I saw the movie, I didn't have much time to organize the client's tutorial, let's write next time.

Multi-Tier structural program development basic tutorial (two

.1)

Below we do the client 1. First create a new project and fill a DATAMODULE. Put the DCOMCONNECTION, CLIENTDATASET, and DATASOURCE controls on DataModule, and their respective attributes are as follows:

Control name

Attributes

DCOMConnection1ServerName set server name Connected: = True; LoginPrompt: = False; ClientDataSet1RemoteServer: = DCOMConnect1ProviderName: = DataSetProvider1 Active: = True; DataSource1DataSet: = ClientDataSet1

2. Go back to Form1, add USES UNIT2 under the Implementation in the code of Form1; enable it to call the content on the DataModule. Drag one of DBGRID1. As shown in Figure: 3. Edit the code of DataModule2, if there is no change name, it is the code of Unit2. Find public, declare a process, enter the next line: procedure applydates; then go to Implementation, write this process, the code is as follows: procedure tdataModule2.ApplyDates; var CustVar: olevariant; begin // This line is automatically submitted Information statement. ClientDataSet1.checkbrowseMode; // The following sentence is to store the modified data into the CustVar variable. If you do not modify the value of CustVar's value (NULL). If ClientDataSet1.changecount> 0 THEN CustVar: = ClientDataSet1.delta else CustVar: = null; // This sentence needs to be added to Variants in the above. Try // Take the ApplyUpdates of the Custvar Biographical Server by DCOMConnection, which is written by us. forget? Go back and see DCOMCONNECTION1.APPServer.ApplyDates (CustVar); ShowMessage ('has successfully written server database'); // If an error is included, fill in Dialogs Except ShowMessage ('error write "); // Exception handling, if an error is wrong. -------------------------------------------------- ------------------------- The above code is the code of the client upload data, and the Custvar variable corresponds to the Custvar variable set by the server, two The variable name can be different, but it is generally settled. 4. Drake 5 ammonium knones, the property settings and event code are as follows: Double-click the ammonium button to enter the onclick code area, and fill out. Control

Attributes

Onclick event code

Buttion1Caption: = insert Datamodule2.ClientDataSet1.Append; Buttion2Caption: = modify Datamodule2.ClientDataSet1.Edit; Buttion3Caption: = delete Datamodule2.ClientDataSet1.Delete; Buttion4Caption: = write Datamodule2.Applydates; // The message is to just call themselves DataModule2 in Fill in the process. Buttion5caption: = Cancel DataModule2.ClientDataSet1.cancelupdates;

The setting is completed as shown: Ok, all the settings of this client are also completed, run, run, running? ? Oh, don't look wrong with it, or the code is not a mistake. ------------------------------- I am exhausted, I am going to sleep.

Source program download

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

New Post(0)