186 CHAPTER 5 ■ DATAREADER AND DATASET
Table 5-3. Characteristics of DataReaders and DataSets
A DataReader presents data through a direct connection to the data source.
Only one row of data is stored in memory at any one time.
You can’t update a data source through a DataReader.
A DataReader connects to only one data source.
A DataSet takes up a lot more IIS and memory
resources to store all the data, but it doesn’t hold up a database connection until it’s closed. The connection needs to be open only when Fill() is called.
A DataSet lasts only until the page is reloaded (posted back), unless it’s somehow persisted (for example, in a session variable).
CHAPTER 5 ■ DATAREADER AND DATASET 187
• If you’re using a DataSet, be aware of how calling Fill() will work with the DataSet you’re using. Will it create new columns in a DataTable or use the other ones there? Make sure the columns you’re querying for in the database match those in the DataTable you’re targeting.
• Don’t forget that primary keys and relationships in a DataSet won’t be copied over from a database. You must create them in code.
In the next chapter, you’ll begin your exploration of data binding, beginning with inline and list binding.