The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

DataSet Class

Represents an in-memory cache of data.

For a list of all members of this type, see DataSet Members.


[Visual Basic]
Public Class DataSet
   Inherits MarshalByValueComponent
   Implements IListSource, ISupportInitialize, ISerializable
public class DataSet : MarshalByValueComponent, IListSource,
   ISupportInitialize, ISerializable
public __gc class DataSet : public MarshalByValueComponent,
   IListSource, ISupportInitialize, ISerializable
class DataSet extends MarshalByValueComponent implements
   IListSource, ISupportInitialize, ISerializable

Thread Safety

This type is safe for multithreaded read operations. You must synchronize any write operations.


The DataSet, which is an in-memory cache of data retrieved from a data source, is a major component of the ADO.NET architecture. The DataSet consists of a collection of DataTable objects that you can relate to each other with DataRelation objects. You can also enforce data integrity in the DataSet by using the UniqueConstraint and ForeignKeyConstraint objects. For further details about working with DataSet objects, see Creating and Using DataSets.

While DataTable objects contain the data, the DataRelationCollection allows you to navigate though the table hierarchy. The tables are contained in a DataTableCollection accessed through the Tables property. When accessing DataTable objects, note that they are conditionally case-sensitive. For example, if one DataTable is named "mydatatable" and another is named "Mydatatable", a string used to search for one of the tables is regarded as case-sensitive. However, if "mydatatable" exists and "Mydatatable" does not, the search string is regarded as case-insensitive. For more information about working with DataTable objects, see Creating a DataTable.

A DataSet can read and write data and schema as XML documents. The data and schema can then be transported across HTTP and used by any application, on any platform that is XML-enabled. You can save the schema as an XML schema with the WriteXmlSchema method, and both schema and data can be saved using the WriteXml method. To read an XML document that includes both schema and data, use the ReadXml method.

In a typical multiple-tier implementation, the steps for creating and refreshing a DataSet, and in turn, updating the original data are to:

  1. Build and fill each DataTable in a DataSet with data from a data source using a DataAdapter.
  2. Change the data in individual DataTable objects by adding, updating, or deleting DataRow objects.
  3. Invoke the GetChanges method to create a second DataSet that features only the changes to the data.
  4. Call the Update method of the DataAdapter, passing the second DataSet as an argument.
  5. Invoke the Merge method to merge the changes from the second DataSet into the first.
  6. Invoke the AcceptChanges on the DataSet. Alternatively, invoke RejectChanges to cancel the changes.
Note   The DataSet and DataTable objects inherit from MarshalByValueComponent, and support the ISerializable interface for remoting. These are the only ADO.NET objects that can be remoted.


[Visual Basic, C#, C++] The following example consists of several methods that, combined, create and fill a DataSet from the Northwind database installed as a sample database with SQLServer 7.0.

[Visual Basic] 
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Drawing
Imports System.ComponentModel
Imports System.Windows.Forms

public class DataGridSample
   Inherits Form
   Private  ds As DataSet 
   Private myGrid As DataGrid 

   Shared Sub Main
      Application.Run(new DataGridSample())
   End Sub

   public Sub New()
   End Sub

   public Sub InitializeComponent()
      Me.ClientSize = new Size(550, 450)
      myGrid = new DataGrid()
      myGrid.Location = new Point (10,10)
      myGrid.Size = new Size(500, 400)
      myGrid.CaptionText = "Microsoft .NET DataGrid"
      Me.Text = "Visual Basic Grid Example"         
      myGrid.SetDataBinding(ds, "Suppliers")
   End Sub

   Private Sub ConnectToData()
      ' Create the ConnectionString and create a SqlConnection.
      ' Change the data source value to the name of your computer.
      Dim cString As string = "Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=mySQLServer"
      Dim cnNorthwind As SqlConnection  = new SqlConnection(cString)
      ' Create a SqlDataAdapter for the Suppliers table.
      Dim adpSuppliers As SqlDataAdapter = new SqlDataAdapter()
      ' A table mapping tells the adapter what to call the table.
      adpSuppliers.TableMappings.Add("Table", "Suppliers")
      Dim cmdSuppliers As SqlCommand = _
      new SqlCommand("SELECT * FROM Suppliers", cnNorthwind)
      cmdSuppliers.CommandType = CommandType.Text

      adpSuppliers.SelectCommand = cmdSuppliers
      Console.WriteLine("The connection is open.")
      ds = New DataSet("Customers")
      ' Create a second SqlDataAdapter and SqlCommand to get
      ' the Products table, a child table of Suppliers. 
      Dim adpProducts As SqlDataAdapter = new SqlDataAdapter()
      adpProducts.TableMappings.Add("Table", "Products")
      Dim cmdProducts As SqlCommand = _
      new SqlCommand("SELECT * FROM Products", cnNorthwind)
      adpProducts.SelectCommand = cmdProducts
      Console.WriteLine("The connection is closed.")
      ' You must create a DataRelation to link the two tables.
      Dim  dr As DataRelation     
      Dim  dc1 As DataColumn     
      Dim dc2 As DataColumn 
      ' Get the parent and child columns of the two tables.
      dc1 = ds.Tables("Suppliers").Columns("SupplierID")
      dc2 = ds.Tables("Products").Columns("SupplierID")
      dr = new System.Data.DataRelation("suppliers2products", dc1, dc2)
   End Sub
End Class

using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;

public class DataGridSample:Form{
   DataSet ds;
   DataGrid myGrid;

   static void Main(){
      Application.Run(new DataGridSample());

   public DataGridSample(){

   void InitializeComponent(){
      this.ClientSize = new System.Drawing.Size(550, 450);
      myGrid = new DataGrid();
      myGrid.Location = new Point (10,10);
      myGrid.Size = new Size(500, 400);
      myGrid.CaptionText = "Microsoft .NET DataGrid";
      this.Text = "C# Grid Example";
      myGrid.SetDataBinding(ds, "Suppliers");
   void ConnectToData(){
      // Create the ConnectionString and create a SqlConnection.
      // Change the data source value to the name of your computer.
      string cString = "Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=mySQLServer";
      SqlConnection myConnection = new SqlConnection(cString);
      // Create a SqlDataAdapter.
      SqlDataAdapter myAdapter = new SqlDataAdapter();
      myAdapter.TableMappings.Add("Table", "Suppliers");
      SqlCommand myCommand = new SqlCommand("SELECT * FROM Suppliers",
      myCommand.CommandType = CommandType.Text;
      myAdapter.SelectCommand = myCommand;
      Console.WriteLine("The connection is open");
      ds = new DataSet("Customers");
      // Create a second Adapter and Command.
      SqlDataAdapter adpProducts = new SqlDataAdapter();
      adpProducts.TableMappings.Add("Table", "Products");
      SqlCommand cmdProducts = new SqlCommand("SELECT * FROM Products", 
      adpProducts.SelectCommand = cmdProducts;
      Console.WriteLine("The connection is closed.");
      System.Data.DataRelation dr;
      System.Data.DataColumn dc1;
      System.Data.DataColumn dc2;
      // Get the parent and child columns of the two tables.
      dc1 = ds.Tables["Suppliers"].Columns["SupplierID"];
      dc2 = ds.Tables["Products"].Columns["SupplierID"];
      dr = new System.Data.DataRelation("suppliers2products", dc1, dc2);

#using <mscorlib.dll>
#using <System.Xml.dll>
#using <System.dll>
#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>
#using <System.Data.dll>
using namespace System;
using namespace System::Data;
using namespace System::Data::SqlClient;
using namespace System::Drawing;
using namespace System::Windows::Forms;

public __gc class DataGridSample:public Form{
   DataSet* ds;
   DataGrid* myGrid;


   void InitializeComponent(){
      this->ClientSize = System::Drawing::Size(550, 450);
      myGrid = new DataGrid();
      myGrid->Location = Point (10,10);
      myGrid->Size = System::Drawing::Size(500, 400);
      myGrid->CaptionText = S"Microsoft .NET DataGrid";
      this->Text = S"C# Grid Example";
      myGrid->SetDataBinding(ds, S"Suppliers");
   void ConnectToData(){
      // Create the ConnectionString and create a SqlConnection.
      // Change the data source value to the name of your computer.
      String* cString = S"Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=mySQLServer";
      SqlConnection* myConnection = new SqlConnection(cString);
      // Create a SqlDataAdapter.
      SqlDataAdapter* myAdapter = new SqlDataAdapter();
      myAdapter->TableMappings->Add(S"Table", S"Suppliers");
      SqlCommand* myCommand = new SqlCommand(S"SELECT * FROM Suppliers",
      myCommand->CommandType = CommandType::Text;
      myAdapter->SelectCommand = myCommand;
      Console::WriteLine(S"The connection is open");
      ds = new DataSet(S"Customers");
      // Create a second Adapter and Command.
      SqlDataAdapter* adpProducts = new SqlDataAdapter();
      adpProducts->TableMappings->Add(S"Table", S"Products");
      SqlCommand* cmdProducts = new SqlCommand(S"SELECT * FROM Products", 
      adpProducts->SelectCommand = cmdProducts;
      Console::WriteLine(S"The connection is closed.");
      System::Data::DataRelation* dr;
      System::Data::DataColumn* dc1;
      System::Data::DataColumn* dc2;
      // Get the parent and child columns of the two tables.
      dc1 = ds->Tables->Item[S"Suppliers"]->Columns->Item[S"SupplierID"];
      dc2 = ds->Tables->Item[S"Products"]->Columns->Item[S"SupplierID"];
      dr = new System::Data::DataRelation(S"suppliers2products", dc1, dc2);

int main(){
   Application::Run(new DataGridSample());

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Namespace: System.Data

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: System.Data (in System.Data.dll)

See Also

DataSet Members | System.Data Namespace | OleDbDataAdapter | DataTable | DataRelation | DataRow | DataView | SqlDataAdapter | ForeignKeyConstraint | UniqueConstraint

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
© 2014 Microsoft