Datasets
A Dataset object represents a collection of DataTable objects. Together, these objects mimic the structure of a database, providing a disconnected, in-memory representation of data. Datasets are central to ADO.NET's data access capabilities, enabling efficient handling of data manipulation and synchronization between client and server.
Key Features of Datasets
- Disconnected Architecture: Datasets can be populated with data and then worked with independently of the data source. This is crucial for applications that need to function without a constant database connection, such as mobile or web applications.
- In-Memory Data Representation: A
Datasetcontains one or moreDataTableobjects, each representing a table of data. These tables can be related to each other usingDataRelationobjects, forming a relational structure in memory. - Schema and Data: A
Datasetcan hold both the schema (structure) and the data itself. This makes it versatile for scenarios where you need to transfer complex data structures. - XML Support: Datasets have robust support for reading and writing their contents (both schema and data) to and from XML. This is invaluable for data exchange and persistence.
- Change Tracking: Datasets track changes made to the data within their tables. This allows for efficient updates by sending only the modified rows back to the data source.
Working with Datasets
The process of using a Dataset typically involves the following steps:
- Creating a Dataset: Instantiate a new
Datasetobject. - Populating the Dataset: Use a
DataAdapter(e.g.,SqlDataAdapter,OleDbDataAdapter) to fill theDatasetwith data from a data source. This is usually done by executing a command and mapping the results to aDataTablewithin theDataset. - Accessing Data: Navigate through the
DataTableobjects in theDatasetto access rows and columns. - Manipulating Data: Modify, add, or delete rows within the
DataTableobjects. - Updating the Data Source: Use the
DataAdapterto send any changes made in theDatasetback to the original data source.
Example: Creating and Populating a Dataset
using System.Data;
using System.Data.SqlClient;
// Assume connectionString and sqlQuery are defined
// Create a SqlConnection
using (SqlConnection connection = new SqlConnection(connectionString))
{
// Create a SqlDataAdapter
using (SqlDataAdapter adapter = new SqlDataAdapter(sqlQuery, connection))
{
// Create a Dataset object
DataSet dataSet = new DataSet();
// Fill the Dataset with data
// The data will be placed into a DataTable with a default name like "Table1"
adapter.Fill(dataSet);
// You can specify a table name if you prefer
// adapter.Fill(dataSet, "Customers");
// Now you can access the data
if (dataSet.Tables.Count > 0)
{
DataTable customersTable = dataSet.Tables[0]; // Or dataSet.Tables["Customers"]
foreach (DataRow row in customersTable.Rows)
{
Console.WriteLine($"Customer Name: {row["CustomerName"]}");
}
}
}
}
Note: While
DataSet is powerful for disconnected scenarios and complex data manipulation, for simpler, forward-only data reading scenarios, consider using DataReader for better performance.