Data Adapter Properties and Methods

■ DataAdapter Properties

• SelectCommand

• InsertCommand

• UpdateCommand

• Deletecommand

■ Methods a DataAdapter uses

• GetChanges (a DataSet method)

■ Practice illegal for non-trainer use

Introduction

DataAdapter Properties

Methods a DataAdapter Uses

Although the DataAdapter class contains a great many properties and methods, you will most likely use a certain subset of each.

You use DataAdapters to act on records from a data source. You can specify which actions you want to perform by using one of four DataAdapter properties, which executes a SQL statement or call a stored procedure. The properties are actually objects that are instances of the SqlCommand or OleDbCommand class:

■ SelectCommand. A reference to a SQL statement or stored procedure that retrieves rows from the data source.

■ InsertCommand. A reference to a command for inserting rows.

■ UpdateCommand. A reference to a command for modifying rows.

■ DeleteCommand. A reference to a command for deleting rows.

You use DataAdapter methods to fill a DataSet or to transmit changes in a DataSet table to a corresponding data store. These methods include:

■ Fill. Use the Fill method of a SqlDataAdapter or OleDbDataAdapter to add or refresh rows from a data source and place them in a DataSet table. The Fill method uses the SELECT statement specified by an associated SelectCommand property.

■ Update. Use this method of a DataAdapter object to transmit changes to a DataSet table to the corresponding data source. This method calls the respective INSERT, UPDATE, or DELETE statement for each specified row in a DataSet DataTable.

■ GetChanges. Use this DataSet method to create a new DataSet that contains a copy of changes to a DataSet.

■ Merge. Use this DataSet method to merge two DataSet objects that have similar schemas, one containing the original data, and the other containing only the changed data. This is useful in a middle-tier application which receives data updates from a client and then needs to merge these changes into its own DataSet.

Practice When you create a DataAdapter, you do not necessarily need to create

Command objects for all the data modification commands (SelectCommand, InsertCommand, UpdateCommand, and DeleteCommand).

Describe a scenario where you would only need to create a Command object for the SelectCommand property.

Describe another scenario where you would only need to create two Command objects - one for the SelectCommand property, and one for the UpdateCommand property.

Describe another scenario where you would need to all four Command objects—one each for SelectCommand, InsertCommand, UpdateCommand, and DeleteCommand.

0 0

Post a comment