Now here in this tutorial, I’ll explain the main differences between dataset vs datatable as well as show you how to use dataset or datatable with an example.
In my previous tutorials, I’d explained the difference between dataset and datareader, difference between string and stringbuilder, difference between dataset datareader dataadapter and dataview, difference between executereader executenonquery and executescalar, OOP concepts with examples and other similar tutorials on difference here.
Let’s start with the difference between DataSet vs DataTable.
Difference Between DataSet vs DataTable
Well, you are looking for the difference between dataset vs datatable, Right?
If so, that means that you are asking for the difference between you and your team that play for same team in sports.
Got my point?
If not, then don’t worry, let me explain clearly with an example.
Ultimately, DataSet is a container of one or more DataTable. For instance, Consider DataSet is a captain of your team and DataTables are other players including you in your team. So, it is like you are asking the difference between you and your team.
By the way, it was just an example for fun. 😉
Now come to the point, following are the list of possible differences between DataSet vs DataTable:
|A DataSet contains a collection of one or more database tables which resides in-memory||A DataTable contains a single database table which resides in-memory|
|It has a collection of datatables||It has a collection of rows and columns|
|DataSet is a collection of DataTable objects, so there could be a relation between each other to get specific results||DataTable is a single database table, so there is no possibility about relation|
|It can fetch multiple TableRows at the same time||It can fetch only one TableRow at the same time|
|DataSource objects are serialized in DataSet. For example, Web Services always returns DataSet as the resultset||DataSource objects are not serialized in DataTable|
Using DataTable and DataSet in Asp.net with Example
Use the following code for C#:
//declare collection of datatables DataTable dt1 = new DataTable(); DataTable dt2 = new DataTable(); //declare dataset and add collection of datatables DataSet ds = new DataSet(); ds.Tables.Add(dt1); ds.Tables.Add(dt2);
Use the following code for Vb.net:
'declare collection of datatables Dim dt1 As New DataTable() Dim dt2 As New DataTable() 'declare dataset and add collection of datatables Dim ds As New DataSet() ds.Tables.Add(dt1) ds.Tables.Add(dt2)
That’s it, now I hope you understood the main differences between DataSet and DataTable in .NET. Let me know in the below comment if you’ve any questions or queries regarding the same.