Skip to main content

Difference between DataSet and DataTable in asp.net ?


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 da = new SqlDataAdapter("select * from Student", con);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
DataSet Example:
SqlDataAdapter da= new SqlDataAdapter("select * from student", con);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();

Comments

Popular posts from this blog

Top 10 ASP.NET Web API Interview Questions

What is ASP.NET Web API? ASP.NET Web API is a framework that simplifies building HTTP services for broader range of clients (including browsers as well as mobile devices) on top of .NET Framework. Using ASP.NET Web API, we can create non-SOAP based services like plain XML or JSON strings, etc. with many other advantages including: Create resource-oriented services using the full features of HTTP Exposing services to a variety of clients easily like browsers or mobile devices, etc. What are the Advantages of Using ASP.NET Web API? Using ASP.NET Web API has a number of advantages, but core of the advantages are: It works the HTTP way using standard HTTP verbs like  GET ,  POST ,  PUT ,  DELETE , etc. for all CRUD operations Complete support for routing Response generated in JSON or XML format using  MediaTypeFormatter It has the ability to be hosted in IIS as well as self-host outside of IIS Supports Model binding and Validation Support for OD...

Extension methods in C#

Consider the class C# 1 2 3 4 5 6 7 8 9 10 11 12 13          namespace ExtensionMethod      {          public class testClass {              public string sayHello ( ) {              return "Hello" ;            }        }      }     Invoke the above from your form using C# 1 2 3 4 5 6          testClass test = new testClass ( ) ;      MessageBox . Show ( test . sayHello ( ) ) ;     This will show “Hello” in message box. Consider the scenario where you don...

What is cookie? Advantages and disadvantages of cookies?

What is cookie? A cookie is a small piece of text file stored on user's computer in the form of name-value pair. Cookies are used by websites to keep track of visitors e.g. to keep user information like username etc. If any web application using cookies, Server send cookies and client browser will store it. The browser then returns the cookie to the server at the next time the page is requested. The most common example of using a cookie is to store User information, User preferences, Password Remember Option etc.It is also one of the common and mostly asked interview questions. Some facts about Cookie Here are a few facts to know about cookies: · Cookies are domain specific i.e. a domain cannot read or write to a cookie created by another domain. This is done by the browser for security purpose. · Cookies are browser specific. Each browser stores the cookies in a different location. The cookies are browser specific and so a cookie created in one browser(e.g in Google Chrome...