19.3. Speeding Up Read-Only Data Access

Problem

You want to speed up read-only data access to a database in your application.

Solution

Use a DataReader instead of a DataAdapter to access the data.

Examples 19-7, 19-8 through 19-9 show the .aspx file and VB and C# code-behind files for our application that demonstrates the performance difference between a DataReader and a DataAdapter using the OleDB managed provider. Figure 19-2 shows the output of the application. Refer to Recipe 19.4 for an equivalent example using the SQL Server managed provider.

Discussion

The CLR provides two primary methods for reading data from a database. The first is to use a DataReader, and the second is to use a DataAdapter in conjunction with a DataTable or DataSet.

Measuring data reader and data adapter performance output

Figure 19-2. Measuring data reader and data adapter performance output

The DataReader provides forward, read-only access to the data read from the database. It provides no mechanisms for randomly accessing the data.

A DataAdapter, along with a DataTable or DataSet, provides random access to data. In addition, the data can be changed in the DataTable or DataSet, and the DataAdapter can be used to update the data in the database.

Of the two access methods, the DataReader is the lightest and fastest and is preferable when you need to only read the data, as reflected in the results we show for our sample application in Figure 19-2. Our example reads 10KB ...

Get ASP.NET 2.0 Cookbook, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.