What is ADO.NET?What are the key components of ADO.NET?What are the ADO.NET namespaces?What is the meaning of object pooling?What are the differences between ADO.NET and classic ADO?Explain the architecture of ADO.NET.?What is a Connection String in ADO.NET C#??How do you establish a database connection using ADO.NET?Explain the concept of Connection Pooling in ADO.NET C#.Differentiate between Object pooling and Connection pooling in C#?What is a DataReader in ADO.NET? Explain with C# example?What is the functionality of CommandBehavior.SchemaOnly?Why CommandBehavior.SingleResult flag is used in ADO.NET?What does CommandBehavior.SingleRow do in ADO.NET?How we can get multiple results by DataReader using same connection in C#?How can we force the connection object to close after my DataReader is closed?What is a DataSet in ADO.NET? Explain with C# example?What are typed and un-typed datasets in ADO.NET C#?Write down some of the characteristic of DataSet?What is the difference between dataSet and DataReader?Why is DataSet Slower than DataReader? Explain with Example.How does DataSet handle data in a disconnected environment?What is the Difference between connected and disconnected architectire?Explain HasChanges() method of DataSet in C#.Explain GetChanges() method with detaild C# Example.Explain RejectChanges() method with C# Example.Explain AcceptChanges() method with C# Example.What are the various methods provided by DataSet for XML in C#?What is the purpose of DataAdapter in ADO.NET?Explain the steps involved in retrieving data using DataAdapter.

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.