In ADO.NET, the disconnected model allows for more flexible data management by enabling the application to interact with the database without maintaining an active connection to the data source. The DataSet, DataAdapter, and CommandBuilder are key components used in this disconnected model.
The DataSet is an in-memory representation of data that can hold multiple tables and their relationships. It is disconnected from the database, meaning it doesn't require an open connection to the data source to manipulate data. A DataSet can be populated by a DataAdapter and later updated to the database.
DataTable objects.DataSet:using System;
using System.Data;
using System.Data.SqlClient;
class Program
{
static void Main()
{
// Define connection string
string connectionString = "your_connection_string_here";
// Create a connection to the database
using (SqlConnection connection = new SqlConnection(connectionString))
{
// Create DataAdapter to fetch data
string query = "SELECT * FROM Employees";
SqlDataAdapter dataAdapter = new SqlDataAdapter(query, connection);
// Create DataSet to hold data
DataSet dataSet = new DataSet();
// Fill the DataSet with data from the database
dataAdapter.Fill(dataSet, "Employees");
// Work with the data in the DataSet
foreach (DataRow row in dataSet.Tables["Employees"].Rows)
{
Console.WriteLine($"{row["EmployeeID"]} - {row["Name"]}");
}
}
}
}
DataSet is used to load data from the Employees table. It can be used to display data in the UI or perform manipulations like sorting, filtering, or editing.The DataAdapter is used to bridge the gap between the disconnected DataSet and the database. It is responsible for fetching data from the database and filling the DataSet, and also for updating the database with changes made to the DataSet.
DataSet.DataSet back to the database.SELECT, INSERT, UPDATE, DELETE) to retrieve or modify data.DataAdapter:using System;
using System.Data;
using System.Data.SqlClient;
class Program
{
static void Main()
{
string connectionString = "your_connection_string_here";
string query = "SELECT * FROM Employees";
// Create connection and DataAdapter
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlDataAdapter dataAdapter = new SqlDataAdapter(query, connection);
// Create DataSet
DataSet dataSet = new DataSet();
// Fill DataSet with data
dataAdapter.Fill(dataSet, "Employees");
// Modify the data in the DataSet (Example: Update employee name)
DataRow row = dataSet.Tables["Employees"].Rows[0];
row["Name"] = "Updated Employee Name";
// Set up the update command for the DataAdapter
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
// Update the database with changes made to the DataSet
dataAdapter.Update(dataSet, "Employees");
}
}
}
DataAdapter is used to fetch data from the Employees table and load it into a DataSet. After making changes to the DataSet (like modifying an employee's name), the Update method is used to push those changes back to the database.The CommandBuilder is a helper class used to automatically generate SQL commands for Insert, Update, and Delete operations. It works with a DataAdapter to generate the necessary SQL statements when updating a DataSet. This helps simplify the process of creating commands for operations based on the DataSet schema.
DataAdapter in a disconnected environment.CommandBuilder:using System;
using System.Data;
using System.Data.SqlClient;
class Program
{
static void Main()
{
string connectionString = "your_connection_string_here";
string query = "SELECT * FROM Employees";
// Create connection and DataAdapter
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlDataAdapter dataAdapter = new SqlDataAdapter(query, connection);
// Create CommandBuilder
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
// Create DataSet
DataSet dataSet = new DataSet();
// Fill DataSet with data
dataAdapter.Fill(dataSet, "Employees");
// Modify data in the DataSet
DataRow row = dataSet.Tables["Employees"].Rows[0];
row["Name"] = "New Employee Name";
// Update the database with changes
dataAdapter.Update(dataSet, "Employees");
Console.WriteLine("Database updated successfully.");
}
}
}
SqlCommandBuilder automatically generates the INSERT, UPDATE, and DELETE SQL commands for the DataAdapter based on the Employees table schema. The DataAdapter then uses these commands to push changes from the DataSet back to the database.DataSet with data by executing a SELECT command. It also handles updates by executing the INSERT, UPDATE, or DELETE commands to sync changes back to the database.INSERT, UPDATE, and DELETE SQL commands for the DataAdapter. This eliminates the need for manually writing those SQL commands for each CRUD operation.DataSet is disconnected from the database, the data can be manipulated offline and can be updated later when the application reestablishes the connection.DataSet can be easily bound to UI controls, making it convenient for user interfaces that display data.| Component | Description |
|---|---|
| DataSet | An in-memory representation of data that can hold multiple tables and their relationships. Can be filled from a database and used offline. |
| DataAdapter | Fills the DataSet with data from the database and pushes changes back to the database. |
| CommandBuilder | Automatically generates SQL commands for the DataAdapter to perform INSERT, UPDATE, and DELETE operations. |
In a disconnected environment, these components work together to allow applications to manage and manipulate data offline, then sync changes to the database when necessary.
Open this section to load past papers