Search in Help for developer site.

Friday 8 July 2016

Difference between DataReader, DataSet, DataAdapter and DataTable in C#

Here I have explained the difference between DataReader, DataSet, DataAdapter and DataTable in C#.




DataReader
DataReader as the name suggests reads data. DataReader is used for fetching records from the SQL Query or Stored Procedure i.e. SELECT Operation.
DataReader is the fastest technique to fetch records from database and it works only in Forward direction meaning a row read once cannot be read again.
DataReader is ReadOnly and it fetches one row at a time in memory and hence it has less load on memory.
The Read function of the DataReader reads one row at a time in memory and if a row is read then the function returns True else False.
DataReader requires an open connection in order to execute the SQL statement.

DataAdapter
DataAdapter is used to execute SQL statements and is used to populate the results of SQL Query into a DataSet or DataTable.
DataAdapter gets all the rows of the executed SQL statement at once and populates into DataSet or DataTable in memory and hence DataAdapter is bit slower compared to DataReader.
Since the DataAdapter populates all rows in DataSet or DataTable it can be traversed in both Forward and Backward directions.
DataAdapter makes use of the Fill function to populate the rows of SQL statement into a DataSet or DataTable.
DataAdapter manages the connection internally and does not require to open or close connections explicitly and this feature is termed as Disconnected Architecture.
DataSet
DataSet is in simple terms set of Data i.e. set of DataTables or collection of DataTables i.e. it can hold one or multiple DataTables.
DataSet is mainly used to fetch and hold the records for one or more tables into memory.
A DataAdapter is used to populate DataSet from records returned from an SQL statement and also a DataSet can be created in memory and tables and data can be added to it.
DataSet can also be converted and saved as XML file.
DataTable
A DataTable can hold records of a single Table consisting of rows and columns. A DataTable can be reside within a DataSet.
DataTable is mainly used to fetch and hold the records of one single table into memory.
A DataAdapter is used to populate DataTable from records returned from an SQL statement and also a DataTable can be created in memory and data can be added to it.

2 comments:

  1. Nice difference between DataReader, DataTable, DataSet and DataAdapter.
    Keep it up Saurabh.

    ReplyDelete