Yes, I mean the other ADO.NET data structures. Technically speaking, the DataReader is not a data structure but a data access class. It does not really "contain" data like the DataTable. Also, the DataSet is nothing but a collection of DataTables.
The difference between using a DataReader and other methods comes down to the way that the DataReader works with the database connection. I don't know specifically how it works internally, but the theory is fairly simple. Consider this psuedo code:
Code:
Open connection
Execute SQL query for DataReader (DB engine executes query, prepares result set)
DataReader.Read() (fetch 1 record from database engine result set)
Do something with data
Loop
Close connection
This results in code processing WHILE you are fetching records from the database which occupies 1 database connection from the connection pool. Now what happens if the "Do something with data" code takes some time? You are sitting on an open connection while doing processing.
Now consider this psuedo code:
Code:
Open connection
Execute SQL query to a DataSet/DataTable (DB engine executes query, dumps to DataTable)
Close connection
For each record in result set
Do something with data
Loop
The utilization of the database connection (the most precious resource) is optimized to take the least amount of time. Now your "do something with data" code can take all the time it needs without utilizing an open connection.
One golden rule you will see often with database interactions is "open late, close early", meaning open the connection as late as possible and close it as soon as you can.
-
Peter