Working With DataSet and Its Methods in ADO.NET

Introduction

 
The DataSet class is a very important class of AD.NET. It is used in disconnected architecture. It represent records in the form of Database table (Row and Column) format. It stores record of one or more tables. I this article, I am describing some useful methods of DataSet with code examples. I am going to use a DataGridView to describe DataSet's method in easier way.
 
First of all, create a Windows Forms Application. Then create a DataGridView and fill it with data through a DataSet. Look at the code given below. In this below sample, make sure to change the connection string with your SQL Server connection. You will also need to change the SELECT SQL statement to the SELECT statement of your table and its columns. If you're new to SQL, check out Top 50 SQL Queries.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.Data.SqlClient;  
  10. using System.Data;  
  11.    
  12. namespace dataset  
  13. {  
  14.     public partial class Form1 : Form  
  15.     {  
  16.         public Form1()  
  17.         {  
  18.             InitializeComponent();  
  19.         }  
  20.         SqlDataAdapter da;  
  21.         DataSet ds;  
  22.         string strconn = "Data Source=YourServerName;Initial Catalog=EMP;Integrated Security=True";  
  23.         private void Form1_Load(object sender, EventArgs e)  
  24.         {  
  25.             da = new SqlDataAdapter("select * from userdet", strconn);  
  26.             ds = new System.Data.DataSet();  
  27.             da.Fill(ds);  
  28.             dataGridView1.DataSource = ds.Tables[0];  
  29.         }  
  30.     }  
  31. }  
Output 
The output looks like this in the UI where the table data is displayed in a DataGridView control.
 
methods of dataset 
 
Now work with DataSet's Methods.
 

DataSet Methods

 

AcceptChanges()

 
This method saves changes which are made with via the UI in a DataSet.
 
Create a Button and add the below code in the first code (given above) on the button click event.
  1. private void btnAcceptChanges_Click(object sender, EventArgs e)  
  2. {  
  3.     ds.AcceptChanges();  
  4. }  
Run the application.
 
Output
 
AcceptChanges() method 
 
Now, make some changes in the records of datagridview (dataset's record) and click the "AcceptChanges" button. You will note that all the changes has saved into datagridview (Note here that these changes will not save into database. Only these changes will update the record of DataSet.) I have made some changes in first row and also added last row. Look at below figure.
 
AcceptChanges() method 
 

Clear()

 
This method clears (removes) all rows from a DataSet.

Create a button, set it's text as Clear and write the below code in the click event.
  1. private void btnclear_Click(object sender, EventArgs e)  
  2. {  
  3.     ds.Clear();  
  4. }  
Run the application. 
 
Output
 
clear method of dataset 
 
Click the "Clear" button. You will note that all rows are clear. Look at the below figure.
 
clear method of dataset 
 

Clone()

 
The Clone method copies the structure of a DataSet. Means it copy only schema of the DataSet not a full records of the DataSet.
 
Take another DataGridView and one button in your project and write the following code on button click event.
  1. private void btnclone_Click(object sender, EventArgs e)  
  2. {  
  3.     DataSet daset = ds.Clone();  
  4.     dataGridView2.DataSource = daset.Tables[0];  
  5. }  
Now run the application.
 
Output
 
clone method of dataset 
 
Click the "clone" button. The below figure shows that only structure has copied.
 
clone method of dataset 
 

Copy()

 
The Copy method copies entire data including records and schema of a DataSet. 

Take a button and set it's text as Copy and write the following code on click event.
  1. private void btncopy_Click(object sender, EventArgs e)  
  2. {  
  3.     DataSet daset = ds.Copy();  
  4.     dataGridView2.DataSource = daset.Tables[0];  
  5. }  
Now run the application.
 
Output
 
copy method of dataset in ado.net 
 
Click the Copy button.
 
copy method of dataset 
 

RejectChanges()

 
This method discards changes that are made to the DataSet and sets the DataSet to its previous stage. 
 
Take a button. Set it's text as RejectChanges and write the following code on button click.
  1. private void btnRejectChanges_Click(object sender, EventArgs e)  
  2. {  
  3.     ds.RejectChanges();  
  4. }  
Run the application.
 
Output: (I have made some changes in records of DataSet by modifying and adding records. The below figure shows records into datagridview after some changes made into dataset.)
 
rejectchanges method in ado.net 
 
Click the "RejectChanges" button. It will RollBack the changes with had made into dataset. Look at below figure.
 
rejectchanges method in ado.net  
 

HasChanges()

 
This method returns boolean value to show whether record of DataSet has changed or not. It returns true if any changes have made and false if no any changes made. 
 
Take a button and set it's text as "HasChanges" and write the following code on button click.
  1. private void btnHasChanges_Click(object sender, EventArgs e)  
  2. {  
  3.     if (ds.HasChanges())  
  4.     {  
  5.         MessageBox.Show("Changes Has Made");  
  6.     }  
  7.     if (!ds.HasChanges())  
  8.     {  
  9.         MessageBox.Show("No Change");  
  10.     }  
  11. }  
Run the application.
 
Output
 
haschanges method in ado.net 
 
Now click at "HasChanges" button after doing some changes in dataset record.
 
Output
 
haschanges method in ado.net 
 

GetChanges()

 
This method copies records that have changed or modified.
 
To understand its function, take a button as "GetChanges" and write the following code on it's click event.
  1. private void btnGetChanges_Click(object sender, EventArgs e)  
  2. {  
  3.     DataSet dasetGetChanges = ds.GetChanges();  
  4.     dataGridView2.DataSource = dasetGetChanges.Tables[0];  
  5. }  
Run the application.
 
Output
 
getchanges method of dataset 
 
Now made some changes in records of dataset and click the "GetChanges" button.
 
haschanges method in ado.net
 
Learn more here: DataSet In C# 
 


Similar Articles