DataSet Class

Namespace: System.Data

Assembly: System.Data.dll

Summary

Represents an in-memory cache of data. It consists of one or more DataTable objects, representing tables of data, along with relationships and constraints between them.

Syntax


[SerializableAttribute]
public class DataSet : MarshalByRefObject, IListSource, ISupportInitialize, IEnumerable
            

Properties

CaseSensitive

Gets or sets a value indicating whether string comparisons within the DataSet are case sensitive.

DataSetName

Gets or sets the name of the DataSet.

DefaultViewManager

Gets a DataViewManager for the DataSet.

EnforceConstraints

Gets or sets a value indicating whether to enforce constraints.

ExtendedProperties

Gets a collection that contains custom user code or per-object data.

Methods

Clone()

Creates and returns a copy of this DataSet. Only the structure of the DataSet, including all table, column, and constraint schemas, is copied. Data is not included.

Copy()

Copies both the structure and data for this DataSet.

ReadXml(XmlReader)

Reads an XML schema and data into the DataSet.

WriteXml(XmlWriter)

Writes the current DataSet contents to an XML document.

Remarks

The DataSet class is a core component of the ADO.NET data access architecture. It provides a rich, programmable, in-memory representation of data that allows you to manage a dataset independently of the data source. You can use a DataSet to cache data retrieved from multiple data sources, perform filtering, sorting, and searching on the data, and combine data from various sources.

A DataSet can contain:

The DataSet can be populated with data from a data source using a DataAdapter or can be created and populated manually.

Example

C# ```csharp using System; using System.Data; public class DataSetExample { public static void Main(string[] args) { // Create a new DataSet DataSet dataSet = new DataSet("SampleDataSet"); // Create a DataTable DataTable dataTable = new DataTable("Customers"); // Add columns to the DataTable DataColumn idColumn = new DataColumn("CustomerID", typeof(int)); DataColumn nameColumn = new DataColumn("CustomerName", typeof(string)); dataTable.Columns.Add(idColumn); dataTable.Columns.Add(nameColumn); // Set the primary key dataTable.PrimaryKey = new DataColumn[] { idColumn }; // Add rows to the DataTable DataRow row1 = dataTable.NewRow(); row1["CustomerID"] = 1; row1["CustomerName"] = "Alice Smith"; dataTable.Rows.Add(row1); DataRow row2 = dataTable.NewRow(); row2["CustomerID"] = 2; row2["CustomerName"] = "Bob Johnson"; dataTable.Rows.Add(row2); // Add the DataTable to the DataSet dataSet.Tables.Add(dataTable); // Output the DataSet name and table names Console.WriteLine($"DataSet Name: {dataSet.DataSetName}"); foreach (DataTable table in dataSet.Tables) { Console.WriteLine($"Table Name: {table.TableName}"); foreach (DataRow row in table.Rows) { Console.WriteLine($" ID: {row["CustomerID"]}, Name: {row["CustomerName"]}"); } } } } ```

See Also