GetChanges() method of DataSet in C#
In C#, the GetChanges()
method is a feature provided by the DataSet
class. It is used to retrieve a copy of the DataSet
that contains only the rows that have been modified, added, or deleted since the last time the AcceptChanges()
method was called.
In simpler terms, it helps you identify and extract the changes made to the data within the DataSet
since the last time you confirmed or accepted those changes. This can be helpful when you want to review or apply these changes selectively, perhaps for the purpose of saving them to a database or performing some other data-related operation.
Here's a detailed example that demonstrates the usage of the GetChanges()
method in C#:
using System;
using System.Data;
class Program
{
static void Main()
{
// Create a new DataSet with a DataTable
DataSet dataSet = new DataSet();
DataTable table = new DataTable("Employees");
// Add columns to the DataTable
table.Columns.Add("ID", typeof(int));
table.Columns.Add("Name", typeof(string));
// Add the DataTable to the DataSet
dataSet.Tables.Add(table);
// Fill the DataTable with data
table.Rows.Add(1, "John");
table.Rows.Add(2, "Alice");
// Display the initial data in the DataTable
Console.WriteLine("Initial data:");
DisplayDataTable(table);
// Modify the data in the DataTable
table.Rows[0]["Name"] = "James";
table.Rows.Add(3, "Emily");
table.Rows.RemoveAt(1);
// Get the changes from the DataSet
DataSet changesDataSet = dataSet.GetChanges();
// Display the modified data from the changes DataSet
if (changesDataSet != null)
{
Console.WriteLine("\nModified data:");
foreach (DataTable changesTable in changesDataSet.Tables)
{
Console.WriteLine($"Table: {changesTable.TableName}");
DisplayDataTable(changesTable);
}
}
else
{
Console.WriteLine("\nNo changes made.");
}
}
static void DisplayDataTable(DataTable table)
{
foreach (DataRow row in table.Rows)
{
foreach (DataColumn col in table.Columns)
{
Console.Write(row[col] + "\t");
}
Console.WriteLine();
}
}
}
In this example, we create a DataSet
and add a DataTable
called "Employees" to it. The DataTable
contains two columns: "ID" and "Name". We fill the DataTable
with some initial data.
Next, we display the initial data in the DataTable
using the DisplayDataTable
method.
After that, we modify the data in the DataTable
by changing the name of the first row, adding a new row, and removing the second row.
We then call the GetChanges()
method on the DataSet
to retrieve a new DataSet object (changesDataSet) that contains the modified data.
If there are changes (i.e., changesDataSet is not null), we display the modified data by iterating over each DataTable
in the changes DataSet
and using the DisplayDataTable
method to print the content.
If there are no changes (i.e., changesDataSet is null), we simply print a message indicating that no changes have been made.
The output of the program will show the initial data and the modified data:
Initial data:
ID Name
1 John
2 Alice
Modified data:
Table: Employees
ID Name
1 James
3 Emily
As you can see, the GetChanges()
method allows us to retrieve a DataSet
object (changesDataSet) that contains only the modified data. We can then further process or persist these changes as needed.