Hello friends, In this article I am going to answering very important question asked in every dot net interview.
If you are new to asp.net web development, you might visit -Different type of Classes in C-Sharp
Here in this article, I am going to explain 7-Main differences between Dataset and Datatable in C# , so that freshers can better understand the difference between them.
1). DataTable A DataTable is an table which is collection of columns and rows representing with in memory.
Dataset DataSet is a collection of DataTables representing in memory. 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.
2). 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.
3). DataTable fetches only one Table Row(s) at a time, whereas DataSet can fetch multiple Table Row(s) at same time.
4). DataTable object is lighter than DataSet, Because DataTable contains data from single table whereas DataSet is heavier object that can contain data from multiple tables.
5). In DataTable, there is no Unique Constraint and Foreign Key Constraint objects available, whereas In DataSet, data integrity is enforced by using the Unique Constraint and Foreign Key Constraint objects.
6). In DataTable, DataSource can't be serialized. But DataSet is serialized DataSource. that is why web services can always returns DataSet as a result but not the Data Tables.
7). As DataTable is a single database table, so there is no Data Relation with in it, whereas In DataSet, DataTable objects can be related to each other with Data Relation.
We can create DataTable, Dataset and populate them with Database table data as follows.
Step 1: Create an Web Application with dot net framework.
Step 2: Create an web form with name DateTimeFormatApp.
Step 3: Add Two GridViews as follows.
Step 4: From Solution Explorer (CTRL + ALT + L) right click on DataTableDataSet.aspx and chose Set As Start Page.
Step 5: Press F7 for Code view and copy following code.
DataTable Example:
using System;
using System.Data;
using System.Data.SqlClient;
namespace DateTimeFormatApp
{
public partial class DataTableDataSet : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
BindDataTableGrid();
}
}
private void BindDataTableGrid()
{
SqlConnection con = new SqlConnection("data source=xxxxxxxxxx;initial catalog=xxxxx;persist security info=True;user id=sa;password=xxxxxx;");
if (con.State == System.Data.ConnectionState.Closed)
con.Open();
SqlDataAdapter adp = new SqlDataAdapter("SELECT StatusId,Status FROM dbo.tbStatus", con);
DataTable dt = new DataTable();
adp.Fill(dt);
grdDataTable.DataSource = dt;
grdDataTable.DataBind();
}
}
}
OUTPUT:
StatusId | Status |
---|---|
2 | Draft |
3 | Future |
0 | Pending |
1 | Publish |
DataSet Example:
using System;
using System.Data;
using System.Data.SqlClient;
namespace DateTimeFormatApp
{
public partial class DataTableDataSet : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
BindDataSetGrid();
}
}
private void BindDataSetGrid()
{
SqlConnection con = new SqlConnection("data source=xxxxxxxxxx;initial catalog=xxxxx;persist security info=True;user id=sa;password=xxxxxx;");
if (con.State == System.Data.ConnectionState.Closed)
con.Open();
SqlDataAdapter adp = new SqlDataAdapter("SELECT StatusId,Status FROM SWC.tbStatus", con);
DataSet ds = new DataSet();
adp.Fill(ds);
grdDataSet.DataSource = ds.Tables[0];
grdDataSet.DataBind();
}
}
}
OUTPUT:
StatusId | Status |
---|---|
2 | Draft |
3 | Future |
0 | Pending |
1 | Publish |
If you have any query or question or topic on which, we might have to write an article for your interest or any kind of suggestion regarding this post, Just feel free to write us, by hit add comment button below or contact via Contact Us form.
Your feedback and suggestions will be highly appreciated. Also try to leave comments from your valid verified email account, so that we can respond you quickly.