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
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)
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
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
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
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
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
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
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
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
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.
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
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
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?
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?
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.
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
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
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
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