2. Use the DataSet object
To transfer data between tiers
To manipulate the data without an open
connection
To relate data from multiple sources
To bind data to a Windows form
3. Referring to Records, Fields
Actual data values are held in DataRow
objects
Each Table object in a dataset has a DataRows
collection made up of DataRow objects
Data values are held in the DataRow.Items
collection
Refer to fields by index position or by name (string
value)
4. Setting a Relationship
A DataRelation object describes the
relationship between the tables.
Use the XML schema (.xsd) file for the dataset
to create the relationship – or – use code to
create a relation
DataRelation Object Functions
Relating tables
Setting up constraints
5. Retrieving Related Row(s)
Use the GetParentRow method to retrieve
the matching row from the table on the one
side
The GetChildRows method returns an array of
rows from the many table
May have 0, 1, or more related records
Use Find method to find a row by its primary
key value
6. DataRowState Enumeration
Used for finding, accessing changed rows
The HasChanges Method determines if
changes have been made to a dataset
Returns a boolean value
The GetChanges Method is used to retrieve
datatable rows that have changes
Use an empty argument to retrieve all changed
rows OR specify type of changes using
enumeration values
7. The Binding Source
Controls record position within a table
Assures that all bound controls on a form
display data from the same record
Can bind controls in design time or in code
8. Binding Source Properties
Position property holds the current row
number (zero based)
Count property indicates the number of
records in a table
The Current Property returns the current row
9. Binding Source Methods
Binding Source Events
The CurrentChanged event occurs when a
bound value is changed
The PositionChanged event occurs when a
user navigates to another record
10. Adding an Event Handler
An event handler executes automatically
when an event occurs.
Need to write event handlers for objects
declared in code
Write the procedure you want to execute when
the event occurs, including the sender and
eventargs arguments
Add the handler in another procedure (such as
constructor), which is called a delegate
turns the general procedure written into an event
procedure
11. DataRow Versions
The DataRow object maintains several versions
of its column data
Current, Original, Default, Proposed
If no changes have been made, the Current and
Original versions are the same
When EndEdit executes, the Current version is
replaced by the Proposed
The EndEdit method confirms the changes
Changes are made when the AcceptChanges
method executes
12. Changing Data
Changes are made to the dataset, not to the
original data source.
Execute the data adapter’s Update method
before calling the AcceptChanges method.
13. The AcceptChanges Method
Calls the EndEdit method of the data row
Removes all rows marked for deletion
Makes the adds and edits indicated for the table
Sets the Original version of each changed row to
the Current version
Sets RowState of each row to Unchanged
The RejectChanges replaces Current versions
with the Original versions
After AcceptChanges or RejectChanges
executes, all RowState properties are reset to
Unchanged
14. Data Adapter Update Method
Update writes the changes to the database
using SQL action queries
Uses a group of Command Objects
One each for insert, update and delete
Can use actual queries or refer to stored
procedures
15. When to Update?
Every time an add, edit, delete occurs?
When the program terminates?
Provide a Save option on a menu and prompt
for unsaved changes when the program
terminates?
16. Update Considerations
Where does the application and data reside?
How many users can make changes?
Does the data source need to be up-to-date
at all times?
17. Concurrency
Concurrency control is the process of handling
conflicts in updates by multiple users.
Pessimistic concurrency control – a row is
unavailable from the time the record is retrieved
until the update is complete
Optimistic concurrency control – a row is
unavailable only while an update is in progress
(default)
“Last in wins” – A row is unavailable only when
the update is being made.
18. Parent/Child Relationships
To maintain referential integrity, update in
the following order:
Delete any child records
Insert, update, and delete the parent records
Insert and update the child records
19. Displaying Related Data
Use a DataRelation
Use GetChildRows/GetParentRow methods
Use a Binding Source that uses a Datarelation as its
datasource
Filter rows by
Using a For loop and adding to an array or datable
Use table.Select method
Requery database for related records
Use Parameter Query; or
Use a variable and build a Select statement with
where clause
20. Stored Procedures
Can store compiled SQL statement in
database
Helps reduce security threats
Can reduce data passed across network
Typically work directly with a command
object
Need to set command type
Need to establish connection for command
21. Parameters
Stored procedures accept and return values
through parameters
Input parameters are similar to arguments in
procedural languages
Command object has a parameters collection
to specify parameters to use with stored
procedure
Can use parameters to pass criteria or values
to edit rows