3
Reply

.NET Interview Questions - Differentiate between ‘DataSet’ and ‘SQLDataReader’ in ADO.NET?

    The DataSet class in ADO.Net operates in an entirely disconnected nature, while DataReader is a connection oriented service. DataSet is an in-memory representation of a collection of Database objects including related tables, constraints, and relationships among the tables.

    SQLDATAREADER is forward only approch that is : SQLDATAREADER DR= new sqldatareader() dr.read(); // 1st row dr.read(); //2nd row If 2nd is executed u cant go back and fetch the 1st row result


    In this question the interviewer is expecting two points the first point is dataset is a connect architecture and datareader is a disconnected architecture. Let’s discuss both these points in detail.

    Point 1:- ‘DataSet’ is a Disconnected Architecture while ‘DataReader’ is a Connected Architecture.

    In order to understand that, below is the sample code for ‘DataSet’ where you can see even after calling the “ObjConnection.Close()” the ”Foreach Loop” is still running. This indicates that the ‘DataSet’ works in Disconnected Architecture.
     

     

    Similarly, below is the sample code for ‘DataReader’ where you can see that when “ObjConnection.Close()” is called then the “While Loop” does not execute and throws an error. This indicates that the ‘DataReader’ works only in Connected Architecture.


    Point 2:- ‘DataSet’ is an in memory representation of database by itself whereas ‘SQLDataReader’ is a flat row and column.

    Dataset is an in-memory database with database, tables, columns and rows. While data reader is a flat table with just rows and columns

    If you see the dataset object in your visual studio intellisense you should see the complete object hierarchy as shown in the below figure.

    If you see the ‘sqldatareader’ in intellisense , its flat table with rows and columns as shown in the below image.