Good Day.
Im new in Programming. Please help me about Dataset and Datareader.
I have couple of question.
1. What is the best way to insert/retrieve a data to SQL Server using
Dataset or Datareader with hundreds or thousands records in Windows or Web
Application?
It depends upon many things. A datareader does not 'contain' the data
- it is a link to the DB, so if you want to persist the data across
layers, for example, you would need to iterate through the data
reader, storing the data in some object (or collection of objects) to
pass back to another layer. DataReaders should be short lived - just
used for getting the data and doing something with it.
DataSets on the other hand actually collect the data into an object,
which itself contains information about the data - and the dataset can
be passed between layers, as it is no longer connected to the DB.
2. In the real world of Data Recording on Database, what is commonly used
Dataset or Datareader?
Both!
In some applications I have passed DataSets between layers, and used
them exclusively; however in others, I have used my own objects, which
are populated using a datareader, and then passed around.
In fact, as far as I understand, a DataSet actually uses a DataReader
internally to 'populate itself'
DataSets, though, have overheads - as much information about the
structure is also contained in the dataset (as well as, potentially,
different versions of each record that has been updated) so, if you
are dealing with a large numnber of records, it can be more efficient
to create your own objects which contain the information you require,
populate them using a datareader, then utilise the objects in the
client facing part of the application, and pass them back to the data
layer for updating.
In a web app, all this will happen at the server side - the objects
(datasets or your own) aren't passed to the user's browser, only data
from them travels this route.
In a windows app, this data may need to be passed over the network to
each client- which can mean that object size is important - which may
move you down the route of using your own objects to transport the
data.