
162
|
Chapter 7: Data Access and Data Binding
This is the Title of the Book, eMatter Edition
Copyright © 2008 O’Reilly & Associates, Inc. All rights reserved.
Read method advances the reader to the next available row and returns a Boolean
indicating whether there is more data to read. This makes it ideal for looping
through data. Note that the example uses the Visual Basic .NET Format function
to format the price data with trailing zeros.
Finally, once we’ve read through all the rows in the data reader, we append a
closing
</table> tag to the Text property of the label and close both the data
reader and the connection. It is very important that you close both when using a
data reader, since failing to close either object can negatively impact the scal-
ability of your application by interfering with the built-in connection pooling
mechanism provided by ADO.NET.
Dataset and data adapter
For circumstances when simply reading through a set of rows once is not suffi-
cient, or if you plan to modify data that you’ve retrieved for later updating on the
backend data store, the data reader will not be sufficient to meet your needs. For
these occasions, the
DataSet class (part of the System.Data namespace) and the
SqlDataAdapter provide more functionality and flexibility than the SqlDataReader,
albeit at the cost of additional overhead.
Example 7-2 retrieves the same data as Example 7-1, but uses a
SqlDataAdapter ...