Realization of multi-table updating in PB data windows

Source: Internet
Author: User
Tags commit integer reset rollback table name

PowerBuilder 's Data Window object is its unique intelligent object, its encapsulation is good, the function is formidable, the manifestation form is rich and diverse, for this reason, many MIS developers regard PowerBuilder as the preferred development tool.

Under normal circumstances, a data window can only update a database table, but in the MIS development process, we often encounter this situation: a data window by two or more database tables as a data source, and need to input or modify it, How to give the general solution of multiple table update becomes an unavoidable problem for MIS development personnel. In the process of developing a management information system, the author tries several kinds of dual-table updating methods to choose a better scheme and readers.

First, the concrete steps

1. When the data window is established, choose SQL Select, which can be either tabular, grid, or freeform. Select two tables that need to be entered or modified, where the primary key and Non-empty columns of the two tables must be selected, determine the selection criteria, and establish a connection relationship.

2. Select all the data items in the first table in the menu "Rows/update" as updatable items.

3. Change the tab order value of the two tables to a non-0 value to make it modifiable in the data window.

In order to make the scheme universal, set up the global function f-update-2table, there are five parameters: Dw-obj, table1, table2, Key1, Key2, respectively, representing the data window to be updated, two table table names and two table primary key column names, where dw- obj is of type DataWindow and the remaining four parameters are of type string. The return value of the function is Boolean, and True indicates success, and returning false indicates failure.

Second, the function of thinking

1. Call Update function update for first database table first. Note the use of parameters: the first parameter action is to control the data window before the mandatory call Accepttext (), in the data window before updating through validation; The second parameter is the reset of the control update flag, and the update flag is reset when True, and the update flag is not bit when false.

2. Change the Updatetable property of the data window so that it points to the second table, and change the key property of the data item Update property and primary key column of the first table to No, and then change the key property of each data item update property and primary key column of the second table to Yes. This article is from http://bianceng.cn (Getting started with programming)

3. Call the update function to update the second table.

4. After the two tables have been successfully updated, the column properties of the two tables and the primary key properties are changed back to the original state to prepare for the next two-table update call.

Iii. List of procedures

ColName: Data Window Column name
Name1[],name2[]: Two database table checked item column name
N1,N2: Number of selected items in two database tables
I: Loop counter
Columns: Total columns of data window
String colname,name1[],name2[]
Integer columns,i,n1=0,n2=0
The following program: Find out dw-obj two table column name assigned to name1[],name2[]
Columns=integer (dw-obj. Describe (″datawindow.column.count″))
For I=1 to Columns
Colname=upper (dw-obj. Describe (″#″+string (i) +″. Name″))
IF Left (Colname,len (Table1)) =table1 and Colname〈〉key1 THEN
N1=n1+1
Name1[n1]=colname
End IF
IF Left (Colname,len (Table2)) =table2 and Colname〈〉key2 THEN
N2=n2+1
Name2[n2]=colname
End IF
NEXT
The following program: Store Dw-obj, determine the return value (True: Success, False: failed)
IF Dw_obj. Update (true,false) =1 THEN
For I=1 to N1
Dw-obj. Modify (Name1[i]+″. Update=no″)
NEXT
Dw-obj. Modify (Key1+″. Key=no″)
Dw-obj. Modify (″datawindow.table.update
Table=′″+table2+″′″)
For I=1 to N2
Dw-obj. Modify (Name2[i]+″. Update=yes″)
NEXT
Dw-obj. Modify (Key2+″. Key=yes″)
IF Dw-obj. Update () =1 THEN
Commit;
For I=1 to N1
Dw-obj. Modify (Name1[i]+″. Update=yes″)
NEXT
Dw-obj. Modify (Key1+″. Key=yes″)
Dw-obj. Modify (″datawindow.table.updatetable=′″+table1+″′″)
For I=1 to N2
Dw-obj. Modify (Name2[i]+″. Update=no″)
NEXT
Dw-obj. Modify (Key2+″. Key=no″)
Return True
ELSE
Rollback;
Return False
End IF
ELSE
Rollback;
Return False
End IF

Four, call the method

Suppose the window is named W-update, the Data window is dw-1, the database table name and primary key name are T1, T2, K1, K2, the Save button is placed, and the script for the button clicked event is as follows:

dw_1.AcceptText()
IF dw—1.ModifiedCount()〉0 or dw_1.DeletedCount()〉0 THEN
IF MessageBox(″提示信息″,″是否存盘?″,Question!,YesNo!)=1 THEN
IF f—update—2table(dw—1,″t1″,″t2″,″k1″,″k2″) THEN
Commit;
ELSE
Rollback;
END IF
END IF
END IF

To sum up, it can be seen that the scheme has a very good scalability, a little modification can solve the multiple tables as a data source of the Data window synchronization update problem.

Related Article

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.