Disconnected Data Sets
A DataSet stores data in memory, and provides a consistent relational programming model that is the same whatever the original source of the data. Thus, a DataSet contains a collection of tables and relationships between tables. Each table contains a primary key and collections of columns and constraints, which define the schema of the table; and a collection of rows, which make up the data stored in the table.
There are several ways you can populate a data set with a schema and data:
Read the schema and data from a database using a DataAdapter.
Programmatically create the schema, and populate it with data.
Load the data set from an XML representation.
The DataSet itself and the second and third methods for populating it are independent of any particular database. You can write database-independent code that works with a DataAdapter by using the IDbDataAdapter interface.
A data adapter provides a bridge between a disconnected data set and its data source. Each .NET data provider provides its own implementation of the interface IDbDataAdapter. The OLE DB data provider has the class OleDbDataAdapter, and the SQL data provider has the class SqlDataAdapter.
A data adapter has properties for SelectCommand, InsertCommand, UpdateCommand, and DeleteCommand. These properties identify the SQL needed to retrieve data, add data, change data, or remove data from the data source.
A data adapter has the Fill method to place data into a data set. It has the Update command to update the data source using data from the data set.
Sample Application Using Disconnected Scenario
We can illustrate the disconnected scenario with another version of our bank application. This time, rather than keeping a connection open, we use a data adapter to populate a data set. We perform various operations on this data set. When we are all done, we update the data source from the data set.
To emphasize the disconnected nature of this scenario, we supply several parallel commands, so that the data set and database can be updated and displayed independently.
Again, we have two versions. BankDbSql uses a SQL Server data adapter, and BankDbJet uses an OLE DB data adapter to talk to an Access database. As with the programs illustrating the connected scenario, only a few lines of code have to be changed to port the program from one data provider to another.
For a more detailed discussion of disconnected data sets and the sample programs, please download the sample chapter.