DataSet 类

定义

表示数据的内存中缓存。

public ref class DataSet : System::ComponentModel::MarshalByValueComponent, System::ComponentModel::IListSource, System::ComponentModel::ISupportInitialize, System::ComponentModel::ISupportInitializeNotification, System::Runtime::Serialization::ISerializable, System::Xml::Serialization::IXmlSerializable
public ref class DataSet : System::ComponentModel::MarshalByValueComponent, System::ComponentModel::IListSource, System::ComponentModel::ISupportInitialize, System::Runtime::Serialization::ISerializable, System::Xml::Serialization::IXmlSerializable
public ref class DataSet : System::ComponentModel::MarshalByValueComponent, System::ComponentModel::IListSource, System::ComponentModel::ISupportInitializeNotification, System::Runtime::Serialization::ISerializable, System::Xml::Serialization::IXmlSerializable
public class DataSet : System.ComponentModel.MarshalByValueComponent, System.ComponentModel.IListSource, System.ComponentModel.ISupportInitialize, System.ComponentModel.ISupportInitializeNotification, System.Runtime.Serialization.ISerializable, System.Xml.Serialization.IXmlSerializable
[System.Serializable]
public class DataSet : System.ComponentModel.MarshalByValueComponent, System.ComponentModel.IListSource, System.ComponentModel.ISupportInitialize, System.Runtime.Serialization.ISerializable, System.Xml.Serialization.IXmlSerializable
[System.Serializable]
public class DataSet : System.ComponentModel.MarshalByValueComponent, System.ComponentModel.IListSource, System.ComponentModel.ISupportInitializeNotification, System.Runtime.Serialization.ISerializable, System.Xml.Serialization.IXmlSerializable
public class DataSet : System.ComponentModel.MarshalByValueComponent, System.ComponentModel.IListSource, System.ComponentModel.ISupportInitializeNotification, System.Runtime.Serialization.ISerializable, System.Xml.Serialization.IXmlSerializable
type DataSet = class
    inherit MarshalByValueComponent
    interface IListSource
    interface ISupportInitialize
    interface ISupportInitializeNotification
    interface ISerializable
    interface IXmlSerializable
[<System.Serializable>]
type DataSet = class
    inherit MarshalByValueComponent
    interface IListSource
    interface IXmlSerializable
    interface ISupportInitialize
    interface ISerializable
[<System.Serializable>]
type DataSet = class
    inherit MarshalByValueComponent
    interface IListSource
    interface IXmlSerializable
    interface ISupportInitializeNotification
    interface ISupportInitialize
    interface ISerializable
[<System.Serializable>]
type DataSet = class
    inherit MarshalByValueComponent
    interface IListSource
    interface IXmlSerializable
    interface ISupportInitializeNotification
    interface ISerializable
    interface ISupportInitialize
Public Class DataSet
Inherits MarshalByValueComponent
Implements IListSource, ISerializable, ISupportInitialize, ISupportInitializeNotification, IXmlSerializable
Public Class DataSet
Inherits MarshalByValueComponent
Implements IListSource, ISerializable, ISupportInitialize, IXmlSerializable
Public Class DataSet
Inherits MarshalByValueComponent
Implements IListSource, ISerializable, ISupportInitializeNotification, IXmlSerializable
继承
属性
实现

示例

以下示例包含多个方法,这些方法组合、创建和填充了 Northwind 数据库中的 ADataSet

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";
        }
    }
}
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 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 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 New SqlDataAdapter()
            productsAdapter.TableMappings.Add("Table", "Products")

            Dim productsCommand As 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 New 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

注解

DataSet它是从数据源检索到的数据的内存中缓存,是 ADO.NET 体系结构的主要组件。 它 DataSet 包含一个 DataTable 对象集合,你可以与对象相互 DataRelation 关联。 还可以通过使用UniqueConstraintForeignKeyConstraint对象来强制实现数据完整性DataSet。 有关使用 DataSet 对象的详细信息,请参阅 DataSets、DataTables 和 DataViews

DataTable 对象包含数据,则 DataRelationCollection 允许你通过表层次结构进行导航。 表包含在 DataTableCollection 通过属性访问的 Tables 表中。 访问 DataTable 对象时,请注意它们有条件区分大小写。 例如,如果一个 DataTable 名为“mydatatable”,另一个名为“Mydatatable”,则用于搜索其中一个表的字符串被视为区分大小写。 但是,如果“mydatatable”存在且“Mydatatable”不存在,则搜索字符串被视为不区分大小写。 有关使用 DataTable 对象的详细信息,请参阅 创建 DataTable

A DataSet 可以读取和写入 XML 文档的数据和架构。 然后,可以在启用 XML 的任何平台上跨 HTTP 传输数据和架构,并由任何应用程序使用。 可以使用该方法将架构另存为 XML 架构 WriteXmlSchema ,并且可以使用该方法保存 WriteXml 架构和数据。 若要读取包含架构和数据的 XML 文档,请使用 ReadXml 该方法。

在典型的多层实现中,创建和刷新原始 DataSet数据的步骤反过来会更新原始数据:

  1. Build and fill each DataTable in a DataSet with data from a data source using a DataAdapter.

  2. 通过添加、更新或删除DataRow对象来更改单个DataTable对象中的数据。

  3. 调用该方法 GetChanges 以创建一个仅功能对数据所做的更改的第二个 DataSet 方法。

  4. Update DataAdapter调用该方法,将第二DataSet个作为参数传递。

  5. 调用该方法 Merge ,将第二 DataSet 个更改合并到第一个。

  6. AcceptChanges调用 on the DataSet. 或者,调用 RejectChanges 以取消更改。

备注

DataSetDataTable对象继承自MarshalByValueComponent,并支持ISerializable远程处理接口。 这两个对象是唯一可远程处理的 ADO.NET 对象。

备注

从中DataSet继承的类不会由垃圾回收器完成,因为终结器已被禁止。DataSet 派生类可以调用 ReRegisterForFinalize 其构造函数中的方法,以允许垃圾回收器完成该类。

安全注意事项

有关 DataSet 和 DataTable 安全性的信息,请参阅 安全指南

构造函数

DataSet()

初始化 DataSet 类的新实例。

DataSet(SerializationInfo, StreamingContext)

用序列化数据初始化 DataSet 类的新实例。

DataSet(SerializationInfo, StreamingContext, Boolean)

用序列化数据初始化 DataSet 类的新实例。

DataSet(String)

使用给定名称初始化 DataSet 类的新实例。

属性

CaseSensitive

获取或设置一个值,该值指示 DataTable 对象中的字符串比较是否区分大小写。

Container

获取组件的容器。

(继承自 MarshalByValueComponent)
DataSetName

获取或设置当前 DataSet 的名称。

DefaultViewManager

获取 DataSet 所包含的数据的自定义视图,以允许使用自定义的 DataViewManager 进行筛选、搜索和导航。

DesignMode

获取指示组件当前是否处于设计模式的值。

(继承自 MarshalByValueComponent)
EnforceConstraints

获取或设置一个值,该值指示在尝试执行任何更新操作时是否遵循约束规则。

Events

获取附加到该组件的事件处理程序的列表。

(继承自 MarshalByValueComponent)
ExtendedProperties

获取与 DataSet 相关的自定义用户信息的集合。

HasErrors

获取一个值,指示在此 DataTable 中的任何 DataSet 对象中是否存在错误。

IsInitialized

获取一个值,该值指示是否已初始化 DataSet

Locale

获取或设置用于比较表中字符串的区域设置信息。

Namespace

获取或设置 DataSet 的命名空间。

Prefix

获取或设置一个 XML 前缀,该前缀是 DataSet 的命名空间的别名。

Relations

获取用于将表链接起来并允许从父表浏览到子表的关系的集合。

RemotingFormat

获取或设置远程处理期间使用的序列化格式 DataSet

SchemaSerializationMode

获取或设置 SchemaSerializationModeDataSet

Site

获取或设置 ISiteDataSet

Tables

获取包含在 DataSet 中的表的集合。

方法

AcceptChanges()

提交自加载此 DataSet 或上次调用 AcceptChanges() 以来对其进行的所有更改。

BeginInit()

开始初始化在窗体上使用或由另一个组件使用的 DataSet。 初始化发生在运行时。

Clear()

通过移除所有表中的所有行来清除任何数据的 DataSet

Clone()

复制 DataSet 的结构,包括所有 DataTable 架构、关系和约束。 不要复制任何数据。

Copy()

复制该 DataSet 的结构和数据。

CreateDataReader()

为每个 DataTableReader 返回带有一个结果集的 DataTable,顺序与 Tables 集合中表的显示顺序相同。

CreateDataReader(DataTable[])

为每个 DataTableReader 返回带有一个结果集的 DataTable

DetermineSchemaSerializationMode(SerializationInfo, StreamingContext)

确定 SchemaSerializationModeDataSet

DetermineSchemaSerializationMode(XmlReader)

确定 SchemaSerializationModeDataSet

Dispose()

释放由 MarshalByValueComponent 使用的所有资源。

(继承自 MarshalByValueComponent)
Dispose(Boolean)

释放由 MarshalByValueComponent 占用的非托管资源,还可以另外再释放托管资源。

(继承自 MarshalByValueComponent)
EndInit()

结束在窗体上使用或由另一个组件使用的 DataSet 的初始化。 初始化发生在运行时。

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetChanges()

获取 DataSet 的副本,该副本包含自加载以来或自上次调用 AcceptChanges() 以来对该数据集进行的所有更改。

GetChanges(DataRowState)

获取由 DataRowState 筛选的 DataSet 的副本,该副本包含上次加载以来或调用 AcceptChanges() 以来进行的所有更改。

GetDataSetSchema(XmlSchemaSet)

获取数据集的 XmlSchemaSet 的副本。

GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetObjectData(SerializationInfo, StreamingContext)

使用序列化 DataSet 时所需的数据填充序列化信息对象。

GetSchemaSerializable()

返回一个可序列化的 XmlSchema 实例。

GetSerializationData(SerializationInfo, StreamingContext)

从二进制或 XML 流反序列化表数据。

GetService(Type)

获取 IServiceProvider 的实施者。

(继承自 MarshalByValueComponent)
GetType()

获取当前实例的 Type

(继承自 Object)
GetXml()

返回存储在 DataSet 中的数据的 XML 表示形式。

GetXmlSchema()

返回存储在 DataSet 中的数据的 XML 表示形式的 XML 架构。

HasChanges()

获取一个值,该值指示 DataSet 是否有更改,包括新增行、已删除的行或已修改的行。

HasChanges(DataRowState)

获取一个值,该值指示 DataSet 是否有 DataRowState 被筛选的更改,包括新增行、已删除的行或已修改的行。

InferXmlSchema(Stream, String[])

将指定 Stream 中的 XML 架构应用于 DataSet

InferXmlSchema(String, String[])

将指定文件中的 XML 架构应用于 DataSet

InferXmlSchema(TextReader, String[])

将指定 TextReader 中的 XML 架构应用于 DataSet

InferXmlSchema(XmlReader, String[])

将指定 XmlReader 中的 XML 架构应用于 DataSet

InitializeDerivedDataSet()

从二进制或 XML 流反序列化数据集的所有表数据。

IsBinarySerialized(SerializationInfo, StreamingContext)

检查 DataSet 的序列化表示形式的格式。

Load(IDataReader, LoadOption, DataTable[])

使用提供的 DataSet 以数据源的值填充 IDataReader,同时使用 DataTable 实例的数组提供架构和命名空间信息。

Load(IDataReader, LoadOption, FillErrorEventHandler, DataTable[])

使用提供的 DataSet 以数据源的值填充 IDataReader,同时使用 DataTable 实例的数组提供架构和命名空间信息。

Load(IDataReader, LoadOption, String[])

使用所提供的 DataSet,并使用字符串数组为 DataSet 中的表提供名称,从而用来自数据源的值填充 IDataReader

MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
Merge(DataRow[])

DataRow 对象数组合并到当前的 DataSet 中。

Merge(DataRow[], Boolean, MissingSchemaAction)

DataRow 对象数组合并到当前的 DataSet 中,在此过程中,将根据给定的参数保留或放弃在 DataSet 中进行的更改并处理不兼容的架构。

Merge(DataSet)

将指定的 DataSet 及其架构合并到当前 DataSet 中。

Merge(DataSet, Boolean)

将指定的 DataSet 及其架构合并到当前 DataSet 中,在此过程中,将根据给定的参数保留或放弃在此 DataSet 中进行的任何更改。

Merge(DataSet, Boolean, MissingSchemaAction)

将指定的 DataSet 及其架构与当前的 DataSet 合并,在此过程中,将根据给定的参数保留或放弃在当前 DataSet 中的更改并处理不兼容的架构。

Merge(DataTable)

将指定的 DataTable 及其架构合并到当前 DataSet 中。

Merge(DataTable, Boolean, MissingSchemaAction)

将指定的 DataTable 及其架构合并到当前的 DataSet 中,在此过程中,将根据给定的参数保留或放弃在 DataSet 中进行的更改并处理不兼容的架构。

OnPropertyChanging(PropertyChangedEventArgs)

引发 OnPropertyChanging(PropertyChangedEventArgs) 事件。

OnRemoveRelation(DataRelation)

当从 DataRelation 中移除 DataTable 对象时发生。

OnRemoveTable(DataTable)

当从 DataTable 中移除 DataSet 时发生。

RaisePropertyChanging(String)

发送指定的 DataSet 属性将要更改的通知。

ReadXml(Stream)

使用指定的 Stream 将 XML 架构和数据读入 DataSet

ReadXml(Stream, XmlReadMode)

使用指定的 DataSetStream 将 XML 架构和数据读入 XmlReadMode

ReadXml(String)

使用指定的文件将 XML 架构和数据读入 DataSet

ReadXml(String, XmlReadMode)

使用指定的文件和 DataSet 将 XML 架构和数据读入 XmlReadMode

ReadXml(TextReader)

使用指定的 TextReader 将 XML 架构和数据读入 DataSet

ReadXml(TextReader, XmlReadMode)

使用指定的 DataSetTextReader 将 XML 架构和数据读入 XmlReadMode

ReadXml(XmlReader)

使用指定的 XmlReader 将 XML 架构和数据读入 DataSet

ReadXml(XmlReader, XmlReadMode)

使用指定的 DataSetXmlReader 将 XML 架构和数据读入 XmlReadMode

ReadXmlSchema(Stream)

从指定的 Stream 中将 XML 架构读入 DataSet

ReadXmlSchema(String)

从指定的文件中将 XML 架构读入 DataSet

ReadXmlSchema(TextReader)

从指定的 TextReader 中将 XML 架构读入 DataSet

ReadXmlSchema(XmlReader)

从指定的 XmlReader 中将 XML 架构读入 DataSet

ReadXmlSerializable(XmlReader)

忽略特性并返回一个空的数据集。

RejectChanges()

回滚自创建 DataSet 以来或上次调用 AcceptChanges() 以来对其进行的所有更改。

Reset()

清除所有表并从 DataSet 中删除所有关系、外部约束和表。 子类应重写 Reset(),以便将 DataSet 还原到其原始状态。

ShouldSerializeRelations()

获取一个值,该值指示是否应该保持 Relations 属性。

ShouldSerializeTables()

获取一个值,该值指示是否应该保持 Tables 属性。

ToString()

返回包含 Component 的名称的 String(如果有)。 不应重写此方法。

(继承自 MarshalByValueComponent)
WriteXml(Stream)

使用指定的 DataSetStream 写当前数据。

WriteXml(Stream, XmlWriteMode)

使用指定的 StreamXmlWriteMode 写入 DataSet 的当前数据和架构(可选)。 若要写入架构,请将 mode 参数的值设置为 WriteSchema

WriteXml(String)

DataSet 的当前数据写入指定的文件。

WriteXml(String, XmlWriteMode)

使用指定的 XmlWriteModeDataSet 的当前数据和架构(可选)写入指定的文件。 若要写入架构,请将 mode 参数的值设置为 WriteSchema

WriteXml(TextWriter)

使用指定的 DataSetTextWriter 写当前数据。

WriteXml(TextWriter, XmlWriteMode)

使用指定的 TextWriterXmlWriteMode 写入 DataSet 的当前数据和架构(可选)。 若要写入架构,请将 mode 参数的值设置为 WriteSchema

WriteXml(XmlWriter)

DataSet 的当前数据写入指定的 XmlWriter

WriteXml(XmlWriter, XmlWriteMode)

使用指定的 XmlWriterXmlWriteMode 写入 DataSet 的当前数据和架构(可选)。 若要写入架构,请将 mode 参数的值设置为 WriteSchema

WriteXmlSchema(Stream)

DataSet 结构作为 XML 架构写入指定的 Stream 对象。

WriteXmlSchema(Stream, Converter<Type,String>)

DataSet 结构作为 XML 架构写入指定的 Stream 对象。

WriteXmlSchema(String)

将 XML 架构形式的 DataSet 结构写入文件。

WriteXmlSchema(String, Converter<Type,String>)

将 XML 架构形式的 DataSet 结构写入文件。

WriteXmlSchema(TextWriter)

DataSet 结构作为 XML 架构写入指定的 TextWriter 对象。

WriteXmlSchema(TextWriter, Converter<Type,String>)

DataSet 结构作为一个 XML 架构写入指定的 TextWriter

WriteXmlSchema(XmlWriter)

将 XML 架构形式的 DataSet 结构写入 XmlWriter 对象。

WriteXmlSchema(XmlWriter, Converter<Type,String>)

DataSet 结构作为一个 XML 架构写入指定的 XmlWriter

事件

Disposed

添加用于侦听组件的 Disposed 事件的事件处理程序。

(继承自 MarshalByValueComponent)
Initialized

初始化 DataSet 后发生。

MergeFailed

当目标和源 DataRow 的主键值相同且 EnforceConstraints 设置为真时发生。

显式接口实现

IListSource.ContainsListCollection

有关此成员的说明,请参见 ContainsListCollection

IListSource.GetList()

有关此成员的说明,请参见 GetList()

ISerializable.GetObjectData(SerializationInfo, StreamingContext)

使用序列化 DataSet 时所需的数据填充序列化信息对象。

IXmlSerializable.GetSchema()

有关此成员的说明,请参见 GetSchema()

IXmlSerializable.ReadXml(XmlReader)

有关此成员的说明,请参见 ReadXml(XmlReader)

IXmlSerializable.WriteXml(XmlWriter)

有关此成员的说明,请参见 WriteXml(XmlWriter)

适用于

线程安全性

此类型对于多线程读取操作是安全的。 必须同步任何写入操作。

另请参阅