The DataAdapter Class
To extract records from a database and use them to fill a table in a DataSet, you need to use another ADO.NET object: a DataAdapter. The DataAdapter comes in a provider-specific object, so there is a separate DataAdapter class for each provider (such as SqlDataAdapter, OracleDataAdapter, and so on).
The DataAdapter serves as a bridge between a single DataTable in the DataSet and the data source. It contains all the available commands for querying and updating the data source.
To enable the DataAdapter to edit, delete, and add rows, you need to specify Command objects for the UpdateCommand, DeleteCommand, and InsertCommand properties of the DataAdapter. To use the DataAdapter to fill a DataSet, you must set the SelectCommand. The DataAdapter provides three key methods, as listed in Table 8-2.
Table 8-2. DataAdapter Methods
Method
Description
Fill() Adds a DataTable to a DataSet by executing the query in the SelectCommand. If your query returns multiple result sets, this method will add multiple DataTable objects at once. You can also use this method to add data to an existing DataTable.
FillSchemaO Adds a DataTable to a DataSet by executing the query in the SelectCommand and retrieving schema information only. This method doesn't add any data to the DataTable. Instead, it simply preconfigures the DataTable with detailed information about column names, data types, primary keys, and unique constraints.
Update() Examines all the changes in a single DataTable and applies this batch of changes to the data source by executing the appropriate InsertCommand, UpdateCommand, and DeleteCommand operations.
Figure 8-4 shows how a DataAdapter and its Command objects work together with the data source and the DataSet.
- Figure 8-4. How the DataAdapter interacts with the data source
Post a comment