Accessing the database
Before you can display data in a DataWindow control, you must
get the data stored in the data source into that control. The most
common way to get the data is to access a database.
An application goes through several steps in accessing a database:
- Set the appropriate values
for the transaction object. - Connect to the database.
- Set the transaction object for the DataWindow control.
- Retrieve and update data.
- Disconnect from the database.
This
section provides instructions for setting the transaction object
for a DataWindow control and for using the DataWindow object to
retrieve and update data.
To learn more about setting values for the
transaction object, connecting to the database, and disconnecting
from the database, see:
- PowerBuilder Application Techniques
, “Using Transaction
Objects” - Web DataWindow “Specifying the database
connection and retrieving data” - Web ActiveX “Using the DataWindow Transaction
Object control”
Setting the transaction object for the DataWindow control
There are two ways to handle database connections and transactions
for the DataWindow control. You can use:
- Internal
transaction management - A separate transaction object
The two methods provide different levels of control over database
transactions.
If you are displaying a PSR file in the control You do not need to use a transaction object or make a database
connection if you are displaying a PSR file in the DataWindow control.
If you change the DataWindow object If you change the DataWindow object associated with a DataWindow
control during execution, you might need to call the SetTrans or
SetTransObject method again.
PowerBuilder You always need to call one of the methods to set the transaction
object.
Web ActiveX You need to call SetTransObject again only when you are using a
separate transaction object.
These options are described in this section.
Internal transaction management
What it does
When the DataWindow control uses internal transaction management,
it handles connecting, disconnecting, commits, and rollbacks. It automatically performs
connects and disconnects as needed; any errors that occur cause
an automatic rollback.
Whenever the DataWindow needs to access the database (such
as when a Retrieve or Update method is executed), the DataWindow
issues an internal CONNECT statement, does the appropriate data
access, then issues an internal DISCONNECT.
Whether to use it
When not to use it Do not use internal transaction management when:
- Your application requires the best possible performance
Internal transaction management is slow and uses considerable
system resources because it must connect and disconnect for every
database access. - You want control over when a transaction is committed
or rolled back
Because internal transaction management must disconnect after
a database access, any changes are always committed immediately.
When to use it If the number of available connections at your site is limited, you
might want to use internal transaction management because connections are
not held open.
Internal transaction management is appropriate in simple situations
when you are doing pure retrievals (such as in reporting) and do
not need to hold database locks—when application control
over committing or rolling back transactions is not an issue.
How it works
PowerBuilder To use internal transaction management, you specify connection
values for a transaction object, which could be the automatically instantiated
SQLCA. Then you call the SetTrans method, which copies the values
from a specified transaction object to the DataWindow control’s
internal transaction object.
1 |
SQLCA.DBMS = ProfileString("myapp.ini", & |
1 |
"database", "DBMS", " ") |
1 |
... // Set more connection parameters |
1 |
dw_employee.SetTrans(SQLCA) |
1 |
dw_employee.Retrieve( ) |
Connecting to the database When you use SetTrans, you do not need to explicitly code
a CONNECT or DISCONNECT statement in a script. CONNECT and DISCONNECT statements
are automatically issued when needed.
For more information about PowerBuilder transaction objects,
see PowerBuilder Application Techniques
.
Web ActiveX To use internal transaction management, set the transaction properties
for the DataWindow Web ActiveX control instead of using a DataWindow
Transaction Object control. You can set the properties using Param
elements or in a script. This example sets the DbParm property and
calls Retrieve in a script:
1 |
dw_employee.DbParm = |
1 |
"Driver='com.sybase.jdbc.SybDriver', |
1 |
URL='jdbc:sybase:Tds:www.domain.com:7373'"; |
1 |
dw_employee.Retrieve( ); |
For internal transaction management, you do not call SetTransObject.
If you change the DataWindow object during execution, the connection
information is still available and the DataWindow connects as needed.
You can change the connection information by changing the value
of the DbParm property.
Transaction management with a separate transaction
object
How it works
When you use a separate transaction object, you control the
duration of the database transaction. Your scripts explicitly connect
to and disconnect from the database. If the transaction object’s
AutoCommit property is set to false, you also
program when an update is committed or rolled back.
Typically, a script for data retrieval or update involves
these statements:
- Connect
- SetTransObject
- Retrieve or Update
- Commit or Rollback
- Disconnect
In PowerBuilder, you use embedded SQL for connecting and committing.
For the Web ActiveX, the transaction object has methods that perform
these actions.
The transaction object also stores error messages returned
from the database in its properties. You can use the error information
to determine whether to commit or roll back database changes.
When to use it
When the DataWindow control uses a separate transaction object,
you have more control of the database processing and are responsible
for managing the database transaction.
There are several reasons to use a separate transaction object:
- You have several DataWindow controls that connect
to the same database and you want to make one database connection
for all of them, saving the overhead of multiple connections - You want to control transaction processing
- You require the improved performance provided by
keeping database connections open
How it works
PowerBuilder The SetTransObject method associates a transaction object with
the DataWindow control. PowerBuilder has a default transaction object called
SQLCA that is automatically instantiated. You can set its connection properties,
connect, and assign it to the DataWindow control.
The following statement uses SetTransObject to associate the
DataWindow control dw_emp with the default transaction
object (SQLCA):
1 |
// Set connection parameters in the transaction object |
1 |
SQLCA.DBMS = ... |
1 |
SQLCA.database = ... |
1 |
CONNECT USING SQLCA; |
1 |
dw_emp.SetTransObject(SQLCA) |
1 |
dw_emp.Retrieve( ) |
Instead of or in addition to using the predefined SQLCA transaction
object, you can define your own transaction object in a script.
This is necessary if your application needs to connect to more than
one database at the same time.
The following statement uses SetTransObject to associate dw_customer
with a programmer-created transaction object (trans_customer):
1 |
transaction trans_customer |
1 |
trans_customer = CREATE transaction |
1 |
// Set connection parameters in the transaction object |
1 |
trans_customer.DBMS = ... |
1 |
trans_customer.database = ... |
1 |
CONNECT USING trans_customer; |
1 |
dw_customer.SetTransObject(trans_customer) |
1 |
dw_customer.Retrieve( ) |
Web ActiveX To use a separate transaction object for the Web ActiveX,
you add an OBJECT element for the Sybase DataWindow Transaction
Object control to the Web page. You can set its connection properties
using Param elements or a script.
A script that connects and retrieves data would have statements
like these:
1 |
trans_1.Connect( ); |
1 |
dw_employee.SetTransObject( trans_1 ); |
1 |
dw_employee.Retrieve( ); |
1 |
trans_1.Disconnect( ); |
For more information
For more information about database transaction
processing:
- PowerBuilder See the chapter on using transaction objects in Application
Techniques - Web ActiveX See Chapter 8, “Using the DataWindow Web Control
for ActiveX”
For more information about SetTrans and SetTransObject
methods, see the DataWindow Reference
.
Retrieving and updating data
You call the following two methods to access a database through
a DataWindow control:
- Retrieve
- Update
Basic data retrieval
After you have set the transaction object for your DataWindow
control, you can use the Retrieve method to retrieve data from the
database into that control:
1 |
dw_emp.Retrieve( ) |
The Web DataWindow server component has a second form of the
method, RetrieveEx, for use when the method requires arguments.
For more information about retrieving data with the Web DataWindow,
see “Specifying the database
connection and retrieving data” and “Passing page-specific data
to the reloaded page”.
Using retrieval arguments
About retrieval arguments
Retrieval arguments qualify the SELECT statement associated
with the DataWindow object, reducing the rows retrieved according
to some criteria. For example, in the following SELECT statement,
Salary is a retrieval argument defined in the DataWindow painter:
1 |
SELECT Name, emp.sal FROM Employee |
1 |
WHERE emp.sal > :Salary |
When you call the Retrieve method, you supply a value for
Salary. In PowerBuilder, the code looks like this:
1 |
dw_emp.Retrieve( 50000 ) |
Special considerations for each environment
are explained below.
When coding Retrieve with arguments, specify them in the order
in which they are defined in the DataWindow object. Your Retrieve
method can provide more arguments than a particular DataWindow object
expects. Any extra arguments are ignored. This allows you to write
a generic Retrieve that works with several different DataWindow
objects.
Omitting retrieval arguments If your DataWindow object takes retrieval arguments but you
do not pass them in the Retrieve method, the DataWindow control
prompts the user for them when Retrieve is called.
More than 16 arguments
The Retrieve method is limited to 16 arguments in some environments.
PowerBuilder You can specify any number of retrieval arguments.
Web DataWindow You can specify a maximum of 16 arguments using the RetrieveEx
method.
Web ActiveX You can specify a maximum of 16 arguments for Retrieve. If you
need to specify more, use the RetrieveEx method for the Web ActiveX
and pass an array where each array element is a retrieval argument.
Updating data
After users have made changes to data in a DataWindow control,
you can use the Update method to save those changes in the database.
In PowerBuilder, the code looks like this:
1 |
dw_emp.<i>Update</i>() |
Update sends to the database all inserts, changes, and deletions
made in the DataWindow control since the last Update method. When
you are using an external transaction object, you can then commit
(or roll back) those database updates. In PowerBuilder, you use
SQL statements. In the Web ActiveX, you use methods and properties
of the transaction object. In the Web DataWindow client control,
update requests call the update method in the server component, which
handles the commit or rollback.
For more specifics on how a DataWindow control
updates the database (that is, which SQL statements are sent in
which situations), see “Updating the database”.
Examples
The following example shows code that connects, retrieves,
updates, commits or rolls back, and disconnects from the database.
Although the example shows all database operations in a single
script or function, most applications separate these operations.
In a PowerBuilder application, for example, an application could
connect to the database in the application Open event, retrieve
and update data in one or more window scripts, and disconnect from
the database in the application Close event.
PowerBuilder The following statements retrieve and update data using the transaction
object EmpSQL and the DataWindow control dw_emp:
1 |
// Connect to the database specified in the |
1 |
// transaction object EmpSQL |
1 |
CONNECT USING EmpSQL; |
1 |
1 |
// Set EmpSQL as the transaction object for dw_emp |
1 |
dw_emp.SetTransObject(EmpSQL) |
1 |
1 |
// Retrieve data from the database specified in |
1 |
// EmpSQL into dw_emp |
1 |
dw_emp.Retrieve() |
1 |
1 |
// Make changes to the data... |
1 |
... |
1 |
1 |
// Update the database |
1 |
IF dw_emp.Update() > 0 THEN |
1 |
COMMIT USING EmpSQL; |
1 |
ELSE |
1 |
ROLLBACK USING EmpSQL; |
1 |
END IF |
1 |
1 |
// Disconnect from the database |
1 |
DISCONNECT USING EmpSQL; |
Web ActiveX The following JavaScript statements retrieve and update data using
the transaction object EmpSQL and the DataWindow control dw_emp.
1 |
// Connect to the database specified in the |
1 |
// transaction object EmpSQL |
1 |
EmpSQL.Connect( ); |
1 |
1 |
// Set EmpSQL as the transaction object for dw_emp |
1 |
dw_emp.SetTransObject(EmpSQL); |
1 |
1 |
// Retrieve data from the database specified in |
1 |
// EmpSQL into dw_emp |
1 |
dw_emp.Retrieve(); |
1 |
1 |
// Make changes to the data |
1 |
... |
1 |
1 |
// Update the database |
1 |
if (dw_emp.Update() > 0) { |
1 |
EmpSQL.Commit( ); |
1 |
} else { |
1 |
EmpSQL.Rollback( ); |
1 |
} |
1 |
1 |
// Disconnect from the database |
1 |
EmpSQL.Disconnect( ); |
Handling retrieval or update errors
A production application should include error tests after
each database operation.
For more about checking for errors, see “Handling DataWindow
errors”.