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

取得或設定值,指出在嘗試任何更新作業時,是否遵循條件約束 (Constraint) 規則。

Events

取得附加在這個元件上的事件處理常式清單。

(繼承來源 MarshalByValueComponent)
ExtendedProperties

取得與 DataSet 相關聯的自訂使用者資訊集合。

HasErrors

取得值,指出這個 DataTable 內的 DataSet 物件中是否有任何一個有錯誤。

IsInitialized

取得值,指出 DataSet 是否已初始化。

Locale

取得或設定用來在資料表中比較字串的地區設定 (Locale) 資訊。

Namespace

取得或設定 DataSet 的命名空間。

Prefix

取得或設定建立 DataSet 命名空間別名的 XML 前置詞。

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 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。

(繼承來源 MarshalByValueComponent)
EndInit()

結束對表單或另一個元件所使用的 DataSet 進行初始化作業。 初始化發生於執行階段。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetChanges()

取得 DataSet (包含從載入它或前一次呼叫 AcceptChanges() 以來所做的所有變更) 的複本。

GetChanges(DataRowState)

取得 DataSet (包含從前一次載入它或呼叫 AcceptChanges() 以來所做的所有變更) 的複本 (由 DataRowState 篩選)。

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,以資料來源的值填入 IDataReader,使用字串的陣列來提供 DataSet 之內的資料表名稱。

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)

使用指定的 DataSet,將 XML 結構描述和資料讀入 Stream

ReadXml(Stream, XmlReadMode)

使用指定的 DataSetStream,將 XML 結構描述和資料讀入 XmlReadMode

ReadXml(String)

使用指定的檔案,將 XML 結構描述和資料讀入 DataSet

ReadXml(String, XmlReadMode)

使用指定的檔案和 DataSet,將 XML 結構描述和資料讀入 XmlReadMode

ReadXml(TextReader)

使用指定的 DataSet,將 XML 結構描述和資料讀入 TextReader

ReadXml(TextReader, XmlReadMode)

使用指定的 DataSetTextReader,將 XML 結構描述和資料讀入 XmlReadMode

ReadXml(XmlReader)

使用指定的 DataSet,將 XML 結構描述和資料讀入 XmlReader

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)

使用指定的 DataSet,寫入 Stream 的目前資料。

WriteXml(Stream, XmlWriteMode)

使用指定的 DataSetStream,寫入 XmlWriteMode 的目前資料,並選擇性寫入結構描述。 若要寫入結構描述,請設定 mode 參數的值為 WriteSchema

WriteXml(String)

DataSet 的目前資料寫入指定的檔案。

WriteXml(String, XmlWriteMode)

使用指定的 DataSet,寫入 XmlWriteMode 的目前資料 (並選擇性寫入結構描述) 至指定的檔案。 若要寫入結構描述,請設定 mode 參數的值為 WriteSchema

WriteXml(TextWriter)

使用指定的 DataSet,寫入 TextWriter 的目前資料。

WriteXml(TextWriter, XmlWriteMode)

使用指定的 DataSetTextWriter,寫入 XmlWriteMode 的目前資料,並選擇性寫入結構描述。 若要寫入結構描述,請設定 mode 參數的值為 WriteSchema

WriteXml(XmlWriter)

DataSet 的目前資料寫入指定的 XmlWriter

WriteXml(XmlWriter, XmlWriteMode)

使用指定的 DataSetXmlWriter,寫入 XmlWriteMode 的目前資料,並選擇性寫入結構描述。 若要寫入結構描述,請設定 mode 參數的值為 WriteSchema

WriteXmlSchema(Stream)

DataSet 結構做為 XML 結構描述寫入指定的 Stream 物件。

WriteXmlSchema(Stream, Converter<Type,String>)

DataSet 結構做為 XML 結構描述寫入指定的 Stream 物件。

WriteXmlSchema(String)

DataSet 結構做為 XML 結構描述寫入檔案。

WriteXmlSchema(String, Converter<Type,String>)

DataSet 結構做為 XML 結構描述寫入檔案。

WriteXmlSchema(TextWriter)

DataSet 結構做為 XML 結構描述寫入指定的 TextWriter 物件。

WriteXmlSchema(TextWriter, Converter<Type,String>)

DataSet 結構當做 XML 結構描述寫入至指定的 TextWriter

WriteXmlSchema(XmlWriter)

DataSet 結構做為 XML 結構描述寫入 XmlWriter 物件。

WriteXmlSchema(XmlWriter, Converter<Type,String>)

DataSet 結構當做 XML 結構描述寫入至指定的 XmlWriter

事件

Disposed

加入事件處理常式來接聽元件上的 Disposed 事件。

(繼承來源 MarshalByValueComponent)
Initialized

發生於 DataSet 初始化之後。

MergeFailed

當目標和來源 DataRow 有相同的主索引鍵值,且 EnforceConstraints 設定為 True 時發生。

明確介面實作

IListSource.ContainsListCollection

如需這個成員的說明,請參閱 ContainsListCollection

IListSource.GetList()

如需這個成員的說明,請參閱 GetList()

ISerializable.GetObjectData(SerializationInfo, StreamingContext)

以序列化 DataSet 所需的資料,填入序列化資訊物件。

IXmlSerializable.GetSchema()

如需這個成員的說明,請參閱 GetSchema()

IXmlSerializable.ReadXml(XmlReader)

如需這個成員的說明,請參閱 ReadXml(XmlReader)

IXmlSerializable.WriteXml(XmlWriter)

如需這個成員的說明,請參閱 WriteXml(XmlWriter)

適用於

執行緒安全性

此類型適用於多線程讀取作業。 您必須同步處理任何寫入作業。

另請參閱