Dataset Brief Explanation - C#.net, asp.net

Dataset: 

dataset is a collection of data tables. We can store many data tables into single dataset.  Dataset acts like set of data table instances.

Dataset is front end memory. by using dataset we can get data from back end to front end and fill into a gridview. 


The Dataset object offers disconnected data source architecture.

Creating dataset:
We can create dataset is simple. Just by new keyword.
Dataset ds=new Dataset ();
If it shows error please add below namespace.
Using  System. Data;

How to fill data into dataset:
To fill table values into dataset  we need to use data adapter . data adapter is used to communicate dataset with database.  Data adapter retrieve the and holds the table values from data base and then fill into dataset by fill() method.
SqlCommand cmd = new SqlCommand("select name,number,location from employee", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);

Assign dataset as data source to grid view:

Above process is same. After filling dataset form data adapter. Add dataset to grid view as data source by data source method. 
After the bind the grid view data by data Bind() method. Because it refresh the data table.

Gridview1.Datasource=ds;
Gridview1.DataBind();

How to Fill Dataset By Data Table:
First create Data table by new keyword. Then create rows and columns and add values to data table.
DataTable dt = new DataTable("employee");
      dt.Columns.Add("name");
      dt.Columns.Add("id");
      dt.Rows.Add("vikram", 1);
      dt.Rows.Add("Sandeep", 2);

Then create dataset and add data table to dataset.

Dataset ds=new dataset();
ds.Tables.Add(dt);

Copy( ) and Clear( ) Methods in Dataset:

We can make copy for dataset by dataset copy method.

Dataset copy=ds.copy();

By clear() method we can clear the values in dataset.

ds.clear();

Print Dataset values in XML format:

This is important concept of dataset. We can print dataset values into xml format by GetXml() method.
DataTable dt = new DataTable("employee");
      dt.Columns.Add("name");
      dt.Columns.Add("id");
      dt.Rows.Add("vikram", 1);
      dt.Rows.Add("Sandeep", 2);

Dataset ds=new dataset();
Ds.Tables.Add(dt);
Console.writeLine(ds.GetXml());
Console.Readkey();
OR
SqlCommand cmd = new SqlCommand("select name,number,location from employee", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
Console.writeLine(ds.GetXml());
Console.Readkey();


Dataset is an disconnected architecture. Or it is a sub copy of original database.  Original data maintained by DBMS and sub copy maintained by dataset. If any changes in dataset ,after it will reflect in database.  






No comments