Related Posts Plugin for WordPress, Blogger...

About

Follow Us

Saturday, 7 February 2015

Introduction: 

What is the difference between DataTable and DataSet is one of the most important questions, which is frequently asked in  interview. I have explained some of the main differences between these two.
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 KeyConstraint 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();
Categories: , ,

0 comments:

Post a Comment