c# and ADO.NET interview questions:- Dataset is a disconnect while datareader is connected.

c# and ADO.NET interview questions:- Dataset is a disconnect while datareader is connected.


In this video I am going to focus upon a very important sentence which developers talk about you know when they talk about dataset and data reader whenever developers talk about the differences between dataset and datareader the first thing which developer say is that dataset is a disconnected architecture while datareader is a connected architecture and in this video I am going to explain what exactly this word disconnected and connected means in terms of dataset and datareader Here is a simple sample code I have and you can see that in the sample code we have opened up a connection then we have written a very simple SQL command here you saying select * from tblCustomer We are using adapter here called as obj adapter to fill up a dataset and in the same way we are also using the same command object and we are filling up a data reader as well then we are browsing using dataset and we are also browsing using datareader then we are calling the connection close If I run this I will see that records are displayed by two means the first one is by using dataset and second one is by using datareader When we say that dataset is a disconnected architecture Even tough if connection is close the dataset will still be able to browse records why because the records the all fetched in memory. The time you call this objAdapter. Fill all the records are filled in memory and even though if the SQL Server is not there or if the connection is not there still you will be able to browse the data set in your csharp code on the contrary datareader it needs a live connection with the SQL Server or live connection should be opened or else this read method will not be able to process when we say datareader is a connected architecture it needs the this connection should be opened If I go now and I move this connection.close over here I have you move the connection close before the loop start executing so what will happen here is now Before this connection close I have ensured that the dataset is filled as well as the datareader is filled After this connection is closed he will be able to browse the dataset rows You will not be able to browse datareader It will through up a exception here why because the datareader needs this connection to be opened Run this in a debug mode do a F5 here There my code is running so first is I opened the connection I have prepared a command object with actually selects you know records from tbl customer The first thing I am doing as I am filling the dataset and to fill a dataset you need adapter How I am creating object of adapter and adapter fills the dataset Second before calling the connection.close I am also filling the datareader Now I am calling the connection.close The connection object is not existing There is no live connection of this csharp application with the SQL This foreach loop which actually browser the dataset will execute If you try to browse this it is browsing the rows If I do a quick watch here I should be able to see the customer code here. You can see 1001 In the same way I should be able to see the customer name as well you can see now the connection has closed I am still able to browse the records Dataset is a in memory disconnected record set let’s go to datareader I am on this sentence here objReader.Read If I try to executes this .Read I will get exception Invalid attempt to call a read when the reader is closed For a datareader a live connection is needed or I will say datareader won’t be able to browse to the records That’s why developers always say dataset is a disconnected architecture and datareader is connected architecture Thank you so much I hope this video has clarified the sentence what is it mean by disconnected and connected architecture in and connected architecture in terms of dataset and datareader thank you

17 thoughts on “c# and ADO.NET interview questions:- Dataset is a disconnect while datareader is connected.

Leave a Reply

Leave a Reply

Your email address will not be published. Required fields are marked *