This documentation is archived and is not being maintained.

DataSet Class

Represents an in-memory cache of data.

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

<SerializableAttribute> _
Public Class DataSet _
	Inherits MarshalByValueComponent _
	Implements IListSource, IXmlSerializable, ISupportInitializeNotification, ISupportInitialize,  _
Dim instance As DataSet

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 DataSets, DataTables, and DataViews (ADO.NET).

Whereas 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 (ADO.NET).

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.


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.


Classes inherited from DataSet are not finalized by the garbage collector, because the finalizer has been suppressed in DataSet. The derived class can call the ReRegisterForFinalize method in its constructor to allow the class to be finalized by the garbage collector.

How to: Create a Typed DatasetData Access in Visual Studio
Walkthrough: Displaying Hierarchical Data in a TreeView ControlBuilding ASP .NET Web Applications in Visual Studio

The following example consists of several methods that, combined, create and fill a DataSet from the Northwind database.

Option Explicit On
Option Strict On

Imports System.Data
Imports system.Data.SqlClient

Public Class NorthwindDataSet

    Public Shared Sub Main()
        Dim connectionString As String = _
    End Sub 

    Private Shared Sub ConnectToData( _
        ByVal connectionString As String)

        ' Create a SqlConnection to the Northwind database. 
        Using connection As SqlConnection = New SqlConnection( _

            ' Create a SqlDataAdapter for the Suppliers table. 
            Dim suppliersAdapter As SqlDataAdapter = _
               New SqlDataAdapter()

            ' A table mapping names the DataTable.
            suppliersAdapter.TableMappings.Add("Table", "Suppliers")

            ' Open the connection.
            Console.WriteLine("The SqlConnection is open.")

            ' Create a SqlCommand to retrieve Suppliers data. 
            Dim suppliersCommand As SqlCommand = New SqlCommand( _
               "SELECT SupplierID, CompanyName FROM dbo.Suppliers;", _
            suppliersCommand.CommandType = CommandType.Text

            ' Set the SqlDataAdapter's SelectCommand.
            suppliersAdapter.SelectCommand = suppliersCommand

            ' Fill the DataSet. 
            Dim dataSet As DataSet = New DataSet("Suppliers")

            ' Create a second SqlDataAdapter and SqlCommand to get 
            ' the Products table, a child table of Suppliers.  
            Dim productsAdapter As SqlDataAdapter = _
                New SqlDataAdapter()
            productsAdapter.TableMappings.Add("Table", "Products")

            Dim productsCommand As SqlCommand = New SqlCommand( _
               "SELECT ProductID, SupplierID FROM dbo.Products;", _
            productsAdapter.SelectCommand = productsCommand

            ' Fill the DataSet.

            ' Close the connection.
            Console.WriteLine("The SqlConnection is closed.")

            ' Create a DataRelation to link the two tables 
            ' based on the SupplierID. 
            Dim parentColumn As DataColumn = _
            Dim childColumn As DataColumn = _
            Dim relation As DataRelation = New _
               System.Data.DataRelation("SuppliersProducts", _
               parentColumn, childColumn)

            Console.WriteLine( _
               "The {0} DataRelation has been created.", _
        End Using 

    End Sub 

    Private Shared Function GetConnectionString() As String 
        ' To avoid storing the connection string in your code,   
        ' you can retrieve it from a configuration file. 
        Return "Data Source=(local);Initial Catalog=Northwind;" _
           & "Integrated Security=SSPI;" 
    End Function 
End Class

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

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0