The dataset is memory cache to retrieve the data form database and in dependent to database. The dataset contains collections of datatable. For all time accessing and modifying database values will not be good so we using dataset to do temporary access. In this dataset we can add data's to datatable and also can relate the datatables's. The modification of the datatable will not reflect in database.
Here first we see about how to create dataset?
Syntax:
DataSet datasetname = new DataSet();
Example :
DataSet ds = new DataSet();
Now we created the dataset with name of ds. Hereby we can add multiple data tables to the dataset.
How to add table?
Syntax:
datasetname.Tables.Add(datatable);
Example:
ds.Tables.Add(dt);
Now we added datatable dt in dataset ds. likewise we can add multiple datatables in dataset.
How to Delete Datatable from dataset?
Syntax:
Datasetname.Tables.Remove(datasetname.Tables[position]);
Example:
ds.Tables.Remove(ds.Tables[ds.Tables.Count-1]);
Here we deleted last Datatable from dataset ds.
Here by making relationship between two tables also possible with the use of foreign and primary key.
Example to set primary key for a Datatable:
dt.PrimaryKey = new DataColumn[] { dt.Columns["id"]};