.NET Framework Class Library
DataSet Class

Represents an in-memory cache of data.

Namespace:   System.Data
Assembly:  System.Data (in System.Data.dll)
Syntax
<[%$TOPIC/bwy42y0e_en-us_VS_110_2_0_0_0_0%]> _
Public Class DataSet _
	Inherits [%$TOPIC/bwy42y0e_en-us_VS_110_2_0_0_0_1%] _
	Implements [%$TOPIC/bwy42y0e_en-us_VS_110_2_0_0_0_2%], [%$TOPIC/bwy42y0e_en-us_VS_110_2_0_0_0_3%], [%$TOPIC/bwy42y0e_en-us_VS_110_2_0_0_0_4%], [%$TOPIC/bwy42y0e_en-us_VS_110_2_0_0_0_5%],  _
	[%$TOPIC/bwy42y0e_en-us_VS_110_2_0_0_0_6%]
[[%$TOPIC/bwy42y0e_en-us_VS_110_2_0_1_0_0%]]
public class DataSet : [%$TOPIC/bwy42y0e_en-us_VS_110_2_0_1_0_1%], [%$TOPIC/bwy42y0e_en-us_VS_110_2_0_1_0_2%], 
	[%$TOPIC/bwy42y0e_en-us_VS_110_2_0_1_0_3%], [%$TOPIC/bwy42y0e_en-us_VS_110_2_0_1_0_4%], [%$TOPIC/bwy42y0e_en-us_VS_110_2_0_1_0_5%], [%$TOPIC/bwy42y0e_en-us_VS_110_2_0_1_0_6%]
[[%$TOPIC/bwy42y0e_en-us_VS_110_2_0_2_0_0%]]
public ref class DataSet : public [%$TOPIC/bwy42y0e_en-us_VS_110_2_0_2_0_1%], 
	[%$TOPIC/bwy42y0e_en-us_VS_110_2_0_2_0_2%], [%$TOPIC/bwy42y0e_en-us_VS_110_2_0_2_0_3%], [%$TOPIC/bwy42y0e_en-us_VS_110_2_0_2_0_4%], [%$TOPIC/bwy42y0e_en-us_VS_110_2_0_2_0_5%], [%$TOPIC/bwy42y0e_en-us_VS_110_2_0_2_0_6%]
[<[%$TOPIC/bwy42y0e_en-us_VS_110_2_0_3_0_0%]>]
type DataSet =  
    class 
        inherit [%$TOPIC/bwy42y0e_en-us_VS_110_2_0_3_0_1%] 
        interface [%$TOPIC/bwy42y0e_en-us_VS_110_2_0_3_0_2%] 
        interface [%$TOPIC/bwy42y0e_en-us_VS_110_2_0_3_0_3%] 
        interface [%$TOPIC/bwy42y0e_en-us_VS_110_2_0_3_0_4%] 
        interface [%$TOPIC/bwy42y0e_en-us_VS_110_2_0_3_0_5%] 
        interface [%$TOPIC/bwy42y0e_en-us_VS_110_2_0_3_0_6%] 
    end

The DataSet type exposes the following members.

Constructors
  NameDescription
Public method Supported by the XNA Framework DataSetInitializes a new instance of the DataSet class.
Public method Supported by the XNA Framework DataSet(String)Initializes a new instance of a DataSet class with the given name.
Protected method DataSet(SerializationInfo, StreamingContext)Infrastructure. Initializes a new instance of a DataSet class that has the given serialization information and context.
Protected method DataSet(SerializationInfo, StreamingContext, Boolean)Infrastructure. Initializes a new instance of the DataSet class.
Top
Properties
  NameDescription
Public property Supported by the XNA Framework CaseSensitiveGets or sets a value indicating whether string comparisons within DataTable objects are case-sensitive.
Public property Supported by the XNA Framework ContainerGets the container for the component. (Inherited from MarshalByValueComponent.)
Public property Supported by the XNA Framework DataSetNameGets or sets the name of the current DataSet.
Public property Supported by the XNA Framework DefaultViewManagerGets a custom view of the data contained in the DataSet to allow filtering, searching, and navigating using a custom DataViewManager.
Public property Supported by the XNA Framework DesignModeGets a value indicating whether the component is currently in design mode. (Inherited from MarshalByValueComponent.)
Public property Supported by the XNA Framework EnforceConstraintsGets or sets a value indicating whether constraint rules are followed when attempting any update operation.
Protected property Supported by the XNA Framework EventsGets the list of event handlers that are attached to this component. (Inherited from MarshalByValueComponent.)
Public property Supported by the XNA Framework ExtendedPropertiesGets the collection of customized user information associated with the DataSet.
Public property Supported by the XNA Framework HasErrorsGets a value indicating whether there are errors in any of the DataTable objects within this DataSet.
Public property Supported by the XNA Framework IsInitializedGets a value that indicates whether the DataSet is initialized.
Public property Supported by the XNA Framework LocaleGets or sets the locale information used to compare strings within the table.
Public property Supported by the XNA Framework NamespaceGets or sets the namespace of the DataSet.
Public property Supported by the XNA Framework PrefixGets or sets an XML prefix that aliases the namespace of the DataSet.
Public property Supported by the XNA Framework RelationsGet the collection of relations that link tables and allow navigation from parent tables to child tables.
Public property RemotingFormatGets or sets a SerializationFormat for the DataSet used during remoting.
Public property Supported by the XNA Framework SchemaSerializationModeGets or sets a SchemaSerializationMode for a DataSet.
Public property Supported by the XNA Framework SiteGets or sets an System.ComponentModelISite for the DataSet. (Overrides MarshalByValueComponentSite.)
Public property Supported by the XNA Framework TablesGets the collection of tables contained in the DataSet.
Top
Methods
  NameDescription
Public method Supported by the XNA Framework AcceptChangesCommits all the changes made to this DataSet since it was loaded or since the last time AcceptChanges was called.
Public method Supported by the XNA Framework BeginInitBegins the initialization of a DataSet that is used on a form or used by another component. The initialization occurs at run time.
Public method Supported by the XNA Framework ClearClears the DataSet of any data by removing all rows in all tables.
Public method Supported by the XNA Framework CloneCopies the structure of the DataSet, including all DataTable schemas, relations, and constraints. Does not copy any data.
Public method Supported by the XNA Framework CopyCopies both the structure and data for this DataSet.
Public method Supported by the XNA Framework CreateDataReaderReturns a DataTableReader with one result set per DataTable, in the same sequence as the tables appear in the Tables collection.
Public method Supported by the XNA Framework CreateDataReader(DataTable)Returns a DataTableReader with one result set per DataTable.
Protected method Supported by the XNA Framework DetermineSchemaSerializationMode(XmlReader)Determines the SchemaSerializationMode for a DataSet.
Protected method DetermineSchemaSerializationMode(SerializationInfo, StreamingContext)Determines the SchemaSerializationMode for a DataSet.
Public method Supported by the XNA Framework DisposeReleases all resources used by the MarshalByValueComponent. (Inherited from MarshalByValueComponent.)
Protected method Supported by the XNA Framework Dispose(Boolean)Releases the unmanaged resources used by the MarshalByValueComponent and optionally releases the managed resources. (Inherited from MarshalByValueComponent.)
Public method Supported by the XNA Framework EndInitEnds the initialization of a DataSet that is used on a form or used by another component. The initialization occurs at run time.
Public method Supported by the XNA Framework Equals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected method Supported by the XNA Framework FinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from MarshalByValueComponent.)
Public method Supported by the XNA Framework GetChangesGets a copy of the DataSet that contains all changes made to it since it was loaded or since AcceptChanges was last called.
Public method Supported by the XNA Framework GetChanges(DataRowState)Gets a copy of the DataSet containing all changes made to it since it was last loaded, or since AcceptChanges was called, filtered by DataRowState.
Public method Static member Supported by the XNA Framework GetDataSetSchemaInfrastructure. Gets a copy of XmlSchemaSet for the DataSet.
Public method Supported by the XNA Framework GetHashCodeServes as the default hash function. (Inherited from Object.)
Public method GetObjectDataPopulates a serialization information object with the data needed to serialize the DataSet.
Protected method Supported by the XNA Framework GetSchemaSerializableInfrastructure. Returns a serializable XMLSchema instance.
Protected method GetSerializationDataInfrastructure. Deserializes the table data from the binary or XML stream.
Public method Supported by the XNA Framework GetServiceGets the implementer of the IServiceProvider. (Inherited from MarshalByValueComponent.)
Public method Supported by the XNA Framework GetTypeGets the Type of the current instance. (Inherited from Object.)
Public method Supported by the XNA Framework GetXmlReturns the XML representation of the data stored in the DataSet.
Public method Supported by the XNA Framework GetXmlSchemaReturns the XML Schema for the XML representation of the data stored in the DataSet.
Public method Supported by the XNA Framework HasChangesGets a value indicating whether the DataSet has changes, including new, deleted, or modified rows.
Public method Supported by the XNA Framework HasChanges(DataRowState)Gets a value indicating whether the DataSet has changes, including new, deleted, or modified rows, filtered by DataRowState.
Public method Supported by the XNA Framework InferXmlSchema(Stream, String)Applies the XML schema from the specified Stream to the DataSet.
Public method Supported by the XNA Framework InferXmlSchema(String, String)Applies the XML schema from the specified file to the DataSet.
Public method Supported by the XNA Framework InferXmlSchema(TextReader, String)Applies the XML schema from the specified TextReader to the DataSet.
Public method Supported by the XNA Framework InferXmlSchema(XmlReader, String)Applies the XML schema from the specified XmlReader to the DataSet.
Protected method Supported by the XNA Framework InitializeDerivedDataSetInfrastructure. Deserialize all of the tables data of the DataSet from the binary or XML stream.
Protected method IsBinarySerializedInspects the format of the serialized representation of the DataSet.
Public method Supported by the XNA Framework Load(IDataReader, LoadOption, DataTable)Fills a DataSet with values from a data source using the supplied IDataReader, using an array of DataTable instances to supply the schema and namespace information.
Public method Supported by the XNA Framework Load(IDataReader, LoadOption, String)Fills a DataSet with values from a data source using the supplied IDataReader, using an array of strings to supply the names for the tables within the DataSet.
Public method Supported by the XNA Framework Load(IDataReader, LoadOption, FillErrorEventHandler, DataTable)Fills a DataSet with values from a data source using the supplied IDataReader, using an array of DataTable instances to supply the schema and namespace information.
Protected method Supported by the XNA Framework MemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public method Supported by the XNA Framework Merge(DataRow)Merges an array of DataRow objects into the current DataSet.
Public method Supported by the XNA Framework Merge(DataSet)Merges a specified DataSet and its schema into the current DataSet.
Public method Supported by the XNA Framework Merge(DataTable)Merges a specified DataTable and its schema into the current DataSet.
Public method Supported by the XNA Framework Merge(DataSet, Boolean)Merges a specified DataSet and its schema into the current DataSet, preserving or discarding any changes in this DataSet according to the given argument.
Public method Supported by the XNA Framework Merge(DataRow, Boolean, MissingSchemaAction)Merges an array of DataRow objects into the current DataSet, preserving or discarding changes in the DataSet and handling an incompatible schema according to the given arguments.
Public method Supported by the XNA Framework Merge(DataSet, Boolean, MissingSchemaAction)Merges a specified DataSet and its schema with the current DataSet, preserving or discarding changes in the current DataSet and handling an incompatible schema according to the given arguments.
Public method Supported by the XNA Framework Merge(DataTable, Boolean, MissingSchemaAction)Merges a specified DataTable and its schema into the current DataSet, preserving or discarding changes in the DataSet and handling an incompatible schema according to the given arguments.
Protected method Supported by the XNA Framework OnPropertyChangingRaises the OnPropertyChanging event.
Protected method Supported by the XNA Framework OnRemoveRelationOccurs when a DataRelation object is removed from a DataTable.
Protected method Supported by the XNA Framework OnRemoveTableOccurs when a DataTable is removed from a DataSet.
Protected method Supported by the XNA Framework RaisePropertyChangingSends a notification that the specified DataSet property is about to change.
Public method Supported by the XNA Framework ReadXml(Stream)Reads XML schema and data into the DataSet using the specified System.IOStream.
Public method Supported by the XNA Framework ReadXml(String)Reads XML schema and data into the DataSet using the specified file.
Public method Supported by the XNA Framework ReadXml(TextReader)Reads XML schema and data into the DataSet using the specified System.IOTextReader.
Public method Supported by the XNA Framework ReadXml(XmlReader)Reads XML schema and data into the DataSet using the specified System.XmlXmlReader.
Public method Supported by the XNA Framework ReadXml(Stream, XmlReadMode)Reads XML schema and data into the DataSet using the specified System.IOStream and XmlReadMode.
Public method Supported by the XNA Framework ReadXml(String, XmlReadMode)Reads XML schema and data into the DataSet using the specified file and XmlReadMode.
Public method Supported by the XNA Framework ReadXml(TextReader, XmlReadMode)Reads XML schema and data into the DataSet using the specified System.IOTextReader and XmlReadMode.
Public method Supported by the XNA Framework ReadXml(XmlReader, XmlReadMode)Reads XML schema and data into the DataSet using the specified System.XmlXmlReader and XmlReadMode.
Public method Supported by the XNA Framework ReadXmlSchema(Stream)Reads the XML schema from the specified Stream into the DataSet.
Public method Supported by the XNA Framework ReadXmlSchema(String)Reads the XML schema from the specified file into the DataSet.
Public method Supported by the XNA Framework ReadXmlSchema(TextReader)Reads the XML schema from the specified TextReader into the DataSet.
Public method Supported by the XNA Framework ReadXmlSchema(XmlReader)Reads the XML schema from the specified XmlReader into the DataSet.
Protected method Supported by the XNA Framework ReadXmlSerializableInfrastructure. Ignores attributes and returns an empty DataSet.
Public method Supported by the XNA Framework RejectChangesRolls back all the changes made to the DataSet since it was created, or since the last time DataSetAcceptChanges was called.
Public method Supported by the XNA Framework ResetClears all tables and removes all relations, foreign constraints, and tables from the DataSet. Subclasses should override Reset to restore a DataSet to its original state.
Protected method Supported by the XNA Framework ShouldSerializeRelationsGets a value indicating whether Relations property should be persisted.
Protected method Supported by the XNA Framework ShouldSerializeTablesGets a value indicating whether Tables property should be persisted.
Public method Supported by the XNA Framework ToStringReturns a String containing the name of the Component, if any. This method should not be overridden. (Inherited from MarshalByValueComponent.)
Public method Supported by the XNA Framework WriteXml(Stream)Writes the current data for the DataSet using the specified System.IOStream.
Public method Supported by the XNA Framework WriteXml(String)Writes the current data for the DataSet to the specified file.
Public method Supported by the XNA Framework WriteXml(TextWriter)Writes the current data for the DataSet using the specified TextWriter.
Public method Supported by the XNA Framework WriteXml(XmlWriter)Writes the current data for the DataSet to the specified XmlWriter.
Public method Supported by the XNA Framework WriteXml(Stream, XmlWriteMode)Writes the current data, and optionally the schema, for the DataSet using the specified System.IOStream and XmlWriteMode. To write the schema, set the value for the mode parameter to WriteSchema.
Public method Supported by the XNA Framework WriteXml(String, XmlWriteMode)Writes the current data, and optionally the schema, for the DataSet to the specified file using the specified XmlWriteMode. To write the schema, set the value for the mode parameter to WriteSchema.
Public method Supported by the XNA Framework WriteXml(TextWriter, XmlWriteMode)Writes the current data, and optionally the schema, for the DataSet using the specified TextWriter and XmlWriteMode. To write the schema, set the value for the mode parameter to WriteSchema.
Public method Supported by the XNA Framework WriteXml(XmlWriter, XmlWriteMode)Writes the current data, and optionally the schema, for the DataSet using the specified XmlWriter and XmlWriteMode. To write the schema, set the value for the mode parameter to WriteSchema.
Public method Supported by the XNA Framework WriteXmlSchema(Stream)Writes the DataSet structure as an XML schema to the specified System.IOStream object.
Public method Supported by the XNA Framework WriteXmlSchema(String)Writes the DataSet structure as an XML schema to a file.
Public method Supported by the XNA Framework WriteXmlSchema(TextWriter)Writes the DataSet structure as an XML schema to the specified TextWriter object.
Public method Supported by the XNA Framework WriteXmlSchema(XmlWriter)Writes the DataSet structure as an XML schema to an XmlWriter object.
Public method WriteXmlSchema(Stream, ConverterType, String)Writes the DataSet structure as an XML schema to the specified System.IOStream object.
Public method WriteXmlSchema(String, ConverterType, String)Writes the DataSet structure as an XML schema to a file.
Public method WriteXmlSchema(TextWriter, ConverterType, String)Writes the DataSet structure as an XML schema to the specified TextWriter.
Public method WriteXmlSchema(XmlWriter, ConverterType, String)Writes the DataSet structure as an XML schema to the specified XmlWriter.
Top
Events
  NameDescription
Public event Supported by the XNA Framework DisposedAdds an event handler to listen to the Disposed event on the component. (Inherited from MarshalByValueComponent.)
Public event Supported by the XNA Framework InitializedOccurs after the DataSet is initialized.
Public event Supported by the XNA Framework MergeFailedOccurs when a target and source DataRow have the same primary key value, and EnforceConstraints is set to true.
Top
Explicit Interface Implementations
  NameDescription
Explicit interface implemetation Private property Supported by the XNA Framework IListSourceContainsListCollectionFor a description of this member, see IListSourceContainsListCollection.
Explicit interface implemetation Private method Supported by the XNA Framework IListSourceGetListFor a description of this member, see IListSourceGetList.
Explicit interface implemetation Private method Supported by the XNA Framework IXmlSerializableGetSchemaFor a description of this member, see IXmlSerializableGetSchema.
Explicit interface implemetation Private method Supported by the XNA Framework IXmlSerializableReadXmlFor a description of this member, see IXmlSerializableReadXml.
Explicit interface implemetation Private method Supported by the XNA Framework IXmlSerializableWriteXmlFor a description of this member, see IXmlSerializableWriteXml.
Top
Remarks

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.

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.

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.

NoteNote

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.

NoteNote

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.

Examples

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 = _
            GetConnectionString()
        ConnectToData(connectionString)
    End Sub 

    Private Shared Sub ConnectToData( _
        ByVal connectionString As String)

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

            ' 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.
            connection.Open()
            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;", _
               connection)
            suppliersCommand.CommandType = CommandType.Text

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

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

            ' 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;", _
               connection)
            productsAdapter.SelectCommand = productsCommand

            ' Fill the DataSet.
            productsAdapter.Fill(dataSet)

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

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

            Console.WriteLine( _
               "The {0} DataRelation has been created.", _
               relation.RelationName)
        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
using System;
using System.Data;
using System.Data.SqlClient;

namespace Microsoft.AdoNet.DataSetDemo
{
    class NorthwindDataSet
    {
        static void Main()
        {
            string connectionString = GetConnectionString();
            ConnectToData(connectionString);
        }

        private static void ConnectToData(string connectionString)
        {
            //Create a SqlConnection to the Northwind database. 
            using (SqlConnection connection =
                       new SqlConnection(connectionString))
            {
                //Create a SqlDataAdapter for the Suppliers table.
                SqlDataAdapter adapter = new SqlDataAdapter();

                // A table mapping names the DataTable.
                adapter.TableMappings.Add("Table", "Suppliers");

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

                // Create a SqlCommand to retrieve Suppliers data.
                SqlCommand command = new SqlCommand(
                    "SELECT SupplierID, CompanyName FROM dbo.Suppliers;",
                    connection);
                command.CommandType = CommandType.Text;

                // Set the SqlDataAdapter's SelectCommand.
                adapter.SelectCommand = command;

                // Fill the DataSet.
                DataSet dataSet = new DataSet("Suppliers");
                adapter.Fill(dataSet);

                // Create a second Adapter and Command to get 
                // the Products table, a child table of Suppliers. 
                SqlDataAdapter productsAdapter = new SqlDataAdapter();
                productsAdapter.TableMappings.Add("Table", "Products");

                SqlCommand productsCommand = new SqlCommand(
                    "SELECT ProductID, SupplierID FROM dbo.Products;",
                    connection);
                productsAdapter.SelectCommand = productsCommand;

                // Fill the DataSet.
                productsAdapter.Fill(dataSet);

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

                // Create a DataRelation to link the two tables 
                // based on the SupplierID.
                DataColumn parentColumn =
                    dataSet.Tables["Suppliers"].Columns["SupplierID"];
                DataColumn childColumn =
                    dataSet.Tables["Products"].Columns["SupplierID"];
                DataRelation relation =
                    new System.Data.DataRelation("SuppliersProducts",
                    parentColumn, childColumn);
                dataSet.Relations.Add(relation);
                Console.WriteLine(
                    "The {0} DataRelation has been created.",
                    relation.RelationName);
            }
        }

        static private string GetConnectionString()
        {
            // 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";
        }
    }
}
Version Information

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Platforms

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Thread Safety

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