In this tutorial, I’ll explain the difference between DataSet, DataReader, DataAdapter, DataView in .net c# as well as vb.net with examples.
Just for refreshing your memory, in my previous tutorials, I’d explained the difference between dataset and datareader, difference between dataset and datatable, difference between executereader executenonquery and executescalar, and difference between String and StringBuilder, and OOP concepts with examples along with other similar tutorials. You can definitely check out those too.
Difference Between DataSet, DataReader, DataAdapter, DataView
Following is the detailed difference between each of them:
Dataset is connection-less oriented, that means whenever we bind data from the database it connects indirectly to the database and then disconnected. It has read/write access to data tables. It supports both forward and backward scanning of data.
DataSet is a container of one or more DataTable. More often than not, it will just contain one table, but if you do a query with multiple SELECT statements, the DataSet will contain a table for each.
You have to be careful about how much data you pull into a DataSet because this is an in-memory representation. You can Fill a DataSet using the Fill() method of a DataAdapter.
DataReader is connection-oriented, that means whenever we bind data from the database it must require a connection and after that disconnected from the connection. It has read-only access, we can’t do any transaction on them. It supports forward-only scanning of data, in other words, you can’t go backward.
DataReader fetches one row at a time so very less network cost as compare to DataSet(that fetches all the rows at a time). It is scalable to any number of records, at least in terms of memory pressure, since it only loads one record at a time. One typical way to get a DataReader is by using the ExecuteReader() method of a DbCommand.
DataAdapter is an ADO.NET Data Provider. DataAdapter can perform any SQL operations like Insert, Update, Delete, Select in the Data Source by using SqlCommand to transmit changes back to the database.
DataAdapter provides communication between the Dataset/DataTable and the Datasource. We can use the DataAdapter in combination with the DataSet/DataTable objects. These two objects combine to enable both data access and data manipulation capabilities.
Generally, we use DataView to filter and sort DataTable rows. So we can say that DataView is like a virtual subset of a DataTable. This capability allows you to have two controls bound to the same DataTable, but showing different versions of the data.
For example, one control may be bound to a DataView showing all of the rows in the table, while a second may be configured to display only the rows that have been deleted from the DataTable. The DataTable also has a DefaultView property that returns the default DataView for the table.
Download Sample Code
Best way to understand difference is, go through the above sample code and step through each of the statements by debugging it. That’s it!
What is the life cycle of using them? (e.g: make request then ..? , edit in DataSet)
There is no life cycle exactly but there are set of statements that you need to follow. I recommend you to download the sample (I added separate pages for all 4 methods for beginners guide), and go through the statements. That’s it!
I hope that will clear all of your doubts, in case if you have any doubts come back here and feel free to ask me here.
Many thank’s for clearing some questions and for an easy to understand way to explain.
Thanks for all of the useful articles, and i am here for requesting one more article from your side and i.e. about ASP.NET page lifecycle.
So if you can provide the article soon then it would be very helpfull.