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.
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Data.SqlClient;
- using System.Data;
-
- namespace dataset
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- SqlDataAdapter da;
- DataSet ds;
- string strconn = "Data Source=YourServerName;Initial Catalog=EMP;Integrated Security=True";
- private void Form1_Load(object sender, EventArgs e)
- {
- da = new SqlDataAdapter("select * from userdet", strconn);
- ds = new System.Data.DataSet();
- da.Fill(ds);
- dataGridView1.DataSource = ds.Tables[0];
- }
- }
- }
Output
The output looks like this in the UI where the table data is displayed in a DataGridView control.
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.
- private void btnAcceptChanges_Click(object sender, EventArgs e)
- {
- ds.AcceptChanges();
- }
Run the application.
Output
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.
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.
- private void btnclear_Click(object sender, EventArgs e)
- {
- ds.Clear();
- }
Run the application.
Output
Click the "Clear" button. You will note that all rows are clear. Look at the below
figure.
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.
- private void btnclone_Click(object sender, EventArgs e)
- {
- DataSet daset = ds.Clone();
- dataGridView2.DataSource = daset.Tables[0];
- }
Now run the application.
Output
Click the "clone" button. The below figure shows that only structure has copied.
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.
- private void btncopy_Click(object sender, EventArgs e)
- {
- DataSet daset = ds.Copy();
- dataGridView2.DataSource = daset.Tables[0];
- }
Now run the application.
Output
Click the Copy button.
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.
- private void btnRejectChanges_Click(object sender, EventArgs e)
- {
- ds.RejectChanges();
- }
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.)
Click the
"RejectChanges" button. It will RollBack the
changes with had made into dataset. Look at below figure.
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.
- private void btnHasChanges_Click(object sender, EventArgs e)
- {
- if (ds.HasChanges())
- {
- MessageBox.Show("Changes Has Made");
- }
- if (!ds.HasChanges())
- {
- MessageBox.Show("No Change");
- }
- }
Run the application.
Output
Now click at
"HasChanges" button after doing some changes in
dataset record.
Output
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.
- private void btnGetChanges_Click(object sender, EventArgs e)
- {
- DataSet dasetGetChanges = ds.GetChanges();
- dataGridView2.DataSource = dasetGetChanges.Tables[0];
- }
Run the application.
Output
Now made some changes in records of dataset and
click the "GetChanges" button.