1. Connect to the database
Connecting to the database is the specified transaction object. PowerBuilder provides two functions: settrans () and settransobject ().
Syntax format:
Dw_control.settrans (transactionobject)
Dw_control.settransobject (transactionobject)
Dw_control is the data window control used, and transactionobject is the transaction object to be specified.
One important difference between the two functions is that they are used.The settrans () function does not require any data initialization or transaction object initialization.. You only need to fill in a transaction object here, Pb will automatically complete the initialization of the transaction object and the work of connecting to the database. When using the settransobject () function, you must first connect the transaction object used to the database.
However, this does not mean that the settrans () function is better than the settransobject () function. When using the settrans () function, each time the sequence function is called, it must connect to the database, this function executes the disconnect statement at the end of each transaction. In contrast, using the settransobject () function can maintain an open connection to the database. Therefore, to improve efficiency, the settransobject () function is always used.
If both functions are successful, 1 is returned. If an error occurs,-1 is returned.
Ii. Data Retrieval
Only one function is used to retrieve data, that is, the retrieve () function.
Syntax format:
Dw_control.retrieve ()
If the data window object on the data window control has retrieval parameters, you must add the retrieval parameters when calling this function. The search parameters must be consistent with the sequence defined in the data window object.
This function returns a long integer representing the number of retrieved data rows. If an error occurs,-1 is returned.
3. Update Data
You must use the update () function to modify the data in the data window object and reflect the changes to the database.
Syntax format:
Dw_control.update ()
This update may be successful or fail. Generally, after this function is called, a check is always performed. See the following example:
Int li_return
Li_return = dw_1.update ()
If li_return = 1 then
Commit using sqlca;
Else
Rollback using sqlca;
End if
In this Code, update the data window control. However, updates only write data to the client's memory and are not submitted to the database. If the update is successful, it is committed to the database. If the update fails, it is rolled back to the current transaction.
Row operation
The row operation function inserts, deletes, or selects data in the database.
1. Insert rows
Insert a row in datawindow. You can use the insertrow () function.
Syntax format:
Dw_control.insertrow (rownumber)
Dw_control is the control name of the data window, And rownumber is the row number to be inserted. If this parameter is set to 0, an empty row is inserted in the last row of the current datawindow.
The insertrow () function returns a long integer value to represent the inserted row number. If insertion fails,-1 is returned.
Ii. Delete rows
To delete a row of data in datawindow, use the deleterow () function.
Syntax format:
Dw_control.deleterow (rownumber)
Rownumber indicates the row number to be deleted. If the value is 0, the current row is deleted. If the deletion is successful, 1 is returned. If the deletion fails,-1 is returned.
3. Set the current row
You can use the setrow () function to set the current row of a behavior in datawindow.
Syntax format:
Dw_control.setrow (rownumber)
Rownumber indicates the row number to be set to the current row. If the function returns 1, the operation is successful. If the function returns-1, the operation fails.
4. Get the current row
If you want to obtain the current row of a behavior in datawindow, you can use the getrow () function.
Syntax format:
Dw_control.getrow ()
This function has no parameter. It returns a long integer representing the current row number. If "-1" is returned, the system fails. If 0 is returned, no rows are selected.
5. Select rows
You can use the selectrow () function to highlight a row in datawindow or unhighlight a row.
Syntax format:
Dw_control.selectrow (rownumber, select)
Rownumber indicates the line number to be highlighted or removed, and 0 indicates all rows. Select is a Boolean value. True indicates brightening, and false indicates canceling brightening. If this function returns 1, it indicates success, and if it returns-1, it indicates failure.
If you want to directly set a line as highlighted, you must first cancel the highlighted display status of other lines, using the following two lines of code:
Dw_1.selectrow (0, false)
Dw_1.selectrow (rownumber, true)
6. Obtain the selected row
To obtain the rows highlighted in the current datawindow, use the getselectrow () function.
Syntax format:
Dw_control.getselectrow (rownumber)
Where rownumber is the row number of the row to be searched, and 0 indicates to be searched from the beginning. This function returns a long integer that indicates the row number of the first highlighted row starting from rownumber. If the request fails, 0 is returned.
7. Scroll rows
If a row of data is inserted at the end of datawindow, and the current row is in the center of datawindow, This insertion may not be noticed by users. To change this situation, you can scroll to the end of datawindow to discover new changes. To scroll a row, you can use the scrolltorow () function.
Syntax format:
Dw_control.scrolltorow (rownumber)
If this function returns 1, it indicates success, and if it returns-1, it indicates failure.
The following functions are related to the scrolltorow () function:
Scrollpriorrow (): Scroll up a row
Scrollnextrow (): Scroll down a row
Column Operations
A column operation function selects a specified column and obtains column information.
1. Retrieve Columns
To obtain the current column number, you can use the getcolumn () function. To obtain the current column name, you can use the getcolumnname () function.
Syntax format:
Dw_control.getcolumn ()
Dw_control.getcolumnname ()
The two functions have no parameters. The getcolumn () function returns a long integer value, representing the current column number. The getcolumnname () function returns the column name of the current column. If 0 is returned, no columns are selected and-1 is returned.
Ii. Set Columns
To set a column as the current column in datawindow, you can use the setcolumn () function.
Syntax format:
Dw_control.setcolumn (column)
Column can be either a column number or a column name. If this function returns 1, it indicates success, and if-1 is returned, it indicates failure.
Data Operations
Data operation functions are mainly used to obtain and set data in datawindow.
1. obtain data
If you want to obtain data from the specified rows and columns in datawindow, you need to use functions of the getitem series.
There are five functions in this series, including strings, numbers, dates, dates, times, And decimals.
Syntax format:
Dw_control.getitemstring (rownumber, column)
Dw_control.getitemnumber (rownumber, column)
Dw_control.getitemdate (rownumber, column)
Dw_control.getitemdatetime (rownumber, column)
Dw_control.getitemdecimal (rownumber, column)
The rownumber parameter indicates the row number, and the column can be the column number or column name.
Ii. Set Data
Unlike the function used to obtain data, you only need to use a setitem () function to set the data in the specified row and column in datawindow.
Syntax format:
Dw_control.setitem (rownumber, column, value)
Rownumber indicates the row number, column can be the column number or column name, and value indicates the value to be set. However, this parameter must be consistent with the data type in the row and column specified in datawindow. Otherwise, PowerBuilder reports an error.
When the setitem () function returns 1, it indicates that the operation is successful. If the return value is-1, it indicates that the operation fails.
Iii. Data Sorting
You can use the setsort () and sort () functions to re-sort the data in datawindow and retrieve the data from the database. These two functions are used together to sort datawindow. Specifically, the setsort () function is used to set how to sort, And the sort () function is used to sort the actual datawindow.
Syntax format:
Dw_control.setsort (expression)
Dw_control.sort ()
Expression is a string that represents a sort expression. Its specific value is a column name followed by a space and then "A", indicating ascending order, or "D ", indicates descending order. If multiple columns need to be sorted at the same time, they are separated by commas.
Example: dw_1.setsort ("name a, xh d ")
Both functions return 1 to indicate success, and-1 to indicate failure.
Iv. Data Filtering
If you want to filter data in datawindow without re-retrieving data from the database, you can use the setfilter () and filter () functions. They are used together to filter data. Where
The setfilter () function is used to set filtering conditions. The filter () function is used to filter datawindow.
Syntax format:
Dw_control.setfiter (expression)
Dw_control.fiter ()
Expression is a string that indicates the filtering condition. It is actually a logical expression.
Example:
Dw_1.setfilter ("ID> \ '003 \ 'and name like \ 'wang % \'")
Dw_1.filter ()
Both functions return 1 to indicate success, and-1 to indicate failure.
V. Data check
PowerBuilder provides two functions for Data check: deletecount () and modifiedcount (). They are used to check the data in datawindow from the last update to the present, the number of deleted rows and the number of modified rows. They are generally used in the closequery event of the window to check whether the data in the datawindow of the window has not been saved.
Syntax format:
Dw_control.deletecount ()
DW. Control. modifiedcount ()
They return the number of rows deleted and modified from the last update to the present in datawindow. If no rows are deleted or modified, 0 is returned. If an error occurs,-1 is returned.
In general, if the window contains data window objects that can be modified, the following code is usually used in the closequery event of the window to check whether there is unsaved data in the data window object:
Int li_return
If dw_1.modifiedcount ()> 0 or dw_1.deletedcount ()> 0 then
Li_return = MessageBox ("prompt", "The data has not been saved. Do you want to save it? ", Question !, Yesnocancel !, 3)
Choose case li_return
Case 1
Triggerevent (\ 'UE _ save \')
Return 0
Case 2
Return 0
Case 3
Return 1
End choose
End if
1.
How can I determine the Data Type of a specified field in the data window?
Dw_1.describe ("colname. coltype ")
Return type: String