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
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 数据库创建和填充 DataSet

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

注解

有关此 API 的详细信息,请参阅 DataSet 的补充 API 备注

构造函数

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)

适用于

线程安全性

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

另请参阅