Difference between DataSet and DataTable in asp.net

Introduction: In previous articles i explained the Difference between Delete and Truncate in sql server and 15 main Difference between DataSet and DataReader in asp.net and  Difference between Response.Redirect and Server.Transfer in asp.net and  20 main differences between Stored procedures and Functions in Sql Server.

Similarly What is the difference between DataTable and DataSet is one of the most important questions asked to freshers in interview. I have tried to explain some of the main differences between the two so that freshers can better understand the difference between them.

1)     A DataTable is an in-memory representation of a single database table which has collection of rows and columns whereas a DataSet is an in-memory representation of a database-like structure which has collection of  DataTables.  Whenever you want to fetch data from database, it connects indirectly to the database and create a virtual database in local system and then disconnected from database. 
            OR
A DataTable is an in-memory representation of a single database table. You can think of it as having columns and rows in the same way. A dataset is an in-memory representation of a database-like structure. It can have one or more DataTables and define relations between them, key or any fields etc.

2)     DataTable object is lighter than DataSet object since it contains data from single table whereas DataSet is heavier object that can contain data from multiple tables.


3)     DataTable fetches only one TableRow at a time whereas DataSet can fetch multiple TableRows at a time 

4)     As DataTable is a single database table, so there is no DataRelation object in it whereas In DataSet, DataTable objects can be related to each other with DataRelation objects.

5)     In DataTable, there is no Unique Constraint and Foreign Key Constraint objects available
But In DataSet, data integrity is enforced by using the Unique Constraint and Foreign Key Constraint objects.

6)     In DataTable,  DataSource cannot be  serialized. But DataSet is serialized DataSource .That is why web services can always returns DataSet as the result but not the DataTables.

7)     We can load a single database table into a DataTable and manipulate the data in memory. If we are going to fetch data from a single database table then DataTable is better choice. While DataSet on the other hand can define DataRelations - which define the relationship between DataTables, much like a foreign key relationship can be set up between tables in a database. DataSets, themselves DOES NOT contain any Data. DataSets contain DataTables (which is where any data actually resides), DataRelations, etc, but no data.

8)     Dataset is a collection of tables, which is used in disconnected architecture. Generally to fill DataSet we use fill method of SqlDataAdapter. It can be used for manipulating the data remotely and finally updating the database with the modified data. This way it enables disconnected means of working with data. This improves performance in terms of reducing the number of times a database is accessed for data manipulations.

DataTable Example:
SqlDataAdapter adp = new SqlDataAdapter("select * from SampleTable", con);
DataTable dt = new DataTable();
adp.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();

 DataSet Example:
SqlDataAdapter adp= new SqlDataAdapter("select * from SampleTable", con);
DataSet ds = new DataSet();
adp.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();

Now over to you:

" I hope the difference between dataset and datatable is clear in your mind and If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linked in and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates."
Previous
Next Post »

3 comments

Click here for comments
Muhammad Ali
admin
December 19, 2013 ×

Dear Lalit Raghuvanshi, I have searched the Forums but couldn't find any satisfactory solution. My problem is regarding the Report Viewer Control I have successfully create an MS report (.rdlc) and attached it to Report Viewer control . It works fine as far as i browse the application using Internet Explorer. The Problem occurs when i browse it using non-IE browser (Firfox,chrome, safari etc). The Print Button is not visible. I gave google a try and found that actually there is an ActiveX control associated with the Print routine of Report Viewer, so it is only available for Internet Explorer. I need to print the report by code. One of the solution was to export the report in EMA format and then print it and the other solution was to use the print option of the browser. Both of them are not feasible for me because my reports usually span over multiple pages and above methods only print the currently visible page. Is there any other way to print report without disturbing the report style, directly from report viewer. I am very desperate to find a solution. Please show me a direction. Regards, Muhammad Ali

Reply
avatar
Unknown
admin
August 19, 2016 ×

Nice Description about datatable and dataset

Reply
avatar
September 09, 2016 ×

Thanks for your valuable feedback..stay connected and keep reading..

Reply
avatar

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..