DataSet 類別

定義

代表記憶體中的資料快取。

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
[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
[<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
type DataSet = class
    inherit MarshalByValueComponent
    interface IListSource
    interface ISupportInitialize
    interface ISupportInitializeNotification
    interface ISerializable
    interface 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

備註

欲了解更多關於此 API 的資訊,請參閱 DataSet 的補充 API 備註

建構函式

名稱 Description
DataSet()

初始化 DataSet 類別的新執行個體。

DataSet(SerializationInfo, StreamingContext, Boolean)

使用串行化數據,初始化 DataSet 類別的新實例。

DataSet(SerializationInfo, StreamingContext)

使用串行化數據,初始化 DataSet 類別的新實例。

DataSet(String)

初始化一個新的類別實例 DataSet ,並以該名稱。

屬性

名稱 Description
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

取得或設定 a SchemaSerializationModeDataSet

Site

取得或設定 一個 ISite ,為 DataSet

Tables

取得包含在 DataSet的表格集合。

方法

名稱 Description
AcceptChanges()

會提交自載入或上次DataSet呼叫以來所有變更AcceptChanges()

BeginInit()

開始初始化用於表單或由其他元件使用的 。DataSet 初始化發生在執行時。

Clear()

透過移除所有資料表中的資料列來清除所有 DataSet 資料。

Clone()

複製 的 DataSet結構,包含所有 DataTable 結構、關係與約束。 不會複製任何資料。

Copy()

複製此 DataSet結構與資料。

CreateDataReader()

回傳 DataTableReader 一個,每個 DataTable,一個結果,順序與集合中 Tables 資料表相同。

CreateDataReader(DataTable[])

回傳 a DataTableReader ,每個 DataTable都設定一個結果。

DetermineSchemaSerializationMode(SerializationInfo, StreamingContext)

決定 對 SchemaSerializationModeDataSet

DetermineSchemaSerializationMode(XmlReader)

決定 對 SchemaSerializationModeDataSet

Dispose()

釋放所有由 MarshalByValueComponent.

(繼承來源 MarshalByValueComponent)
Dispose(Boolean)

釋放 未管理的資源, MarshalByValueComponent 並可選擇性地釋放受管理資源。

(繼承來源 MarshalByValueComponent)
EndInit()

結束用於表單或被其他元件使用的 a DataSet 的初始化。 初始化發生在執行時。

Equals(Object)

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

(繼承來源 Object)
GetChanges()

會取得一份包含自載入AcceptChanges()或最後調用以來所有變更的 的DataSet副本。

GetChanges(DataRowState)

會取得一份包含自上次載入或AcceptChanges()被呼叫以來所有變更的副本DataSet,並被 DataRowState過濾。

GetDataSetSchema(XmlSchemaSet)

取得 DataSet 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[], Boolean, MissingSchemaAction)

DataRow 對象的數組合並至目前的 DataSet,保留或捨棄 DataSet 中的變更,並根據指定的自變數處理不相容的架構。

Merge(DataRow[])

DataRow 物件的數組合並至目前的 DataSet

Merge(DataSet, Boolean, MissingSchemaAction)

將指定的 DataSet 及其架構與目前的 DataSet合併,保留或捨棄目前 DataSet 中的變更,並根據指定的自變數處理不相容的架構。

Merge(DataSet, Boolean)

根據指定的自變數,將指定的 DataSet 及其架構合併至目前的 DataSet,保留或捨棄此 DataSet 中的任何變更。

Merge(DataSet)

將指定的 DataSet 及其架構合併至目前的 DataSet

Merge(DataTable, Boolean, MissingSchemaAction)

將指定的 DataTable 及其架構合併至目前的 DataSet,保留或捨棄 DataSet 中的變更,並根據指定的自變數處理不相容的架構。

Merge(DataTable)

將指定的 DataTable 及其架構合併至目前的 DataSet

OnPropertyChanging(PropertyChangedEventArgs)

引發 OnPropertyChanging(PropertyChangedEventArgs) 事件。

OnRemoveRelation(DataRelation)

當物件 DataRelation 從 中移除 DataTable時,會發生。

OnRemoveTable(DataTable)

當 a DataTable 從 中移除 DataSet時,會發生。

RaisePropertyChanging(String)

會發送通知,告知指定的 DataSet 屬性即將變更。

ReadXml(Stream, XmlReadMode)

利用指定的 StreamXmlReadMode讀取 XML 結構與資料。DataSet

ReadXml(Stream)

利用指定的 StreamXML 結構與資料讀取。DataSet

ReadXml(String, XmlReadMode)

利用指定的檔案和 XmlReadMode,將 XML 架構和資料讀入 DataSet

ReadXml(String)

利用指定的檔案讀取 XML 結構與資料。DataSet

ReadXml(TextReader, XmlReadMode)

利用指定的 TextReaderXmlReadMode讀取 XML 結構與資料。DataSet

ReadXml(TextReader)

利用指定的 TextReaderXML 結構與資料讀取。DataSet

ReadXml(XmlReader, XmlReadMode)

利用指定的 XmlReaderXmlReadMode讀取 XML 結構與資料。DataSet

ReadXml(XmlReader)

利用指定的 XmlReaderXML 結構與資料讀取。DataSet

ReadXmlSchema(Stream)

將指定的 Stream XML 架構讀取到 DataSet.

ReadXmlSchema(String)

將指定檔案中的 XML 架構讀取到 DataSet.

ReadXmlSchema(TextReader)

將指定的 TextReader XML 架構讀取到 DataSet.

ReadXmlSchema(XmlReader)

將指定的 XmlReader XML 架構讀取到 DataSet.

ReadXmlSerializable(XmlReader)

忽略屬性並回傳一個空的 DataSet。

RejectChanges()

回滾自建立以來的所有 DataSet 變更,或自上次 AcceptChanges() 呼叫以來所做的變更。

Reset()

清除所有資料表,並移除所有關係、外部約束及資料表。DataSet 子職業應該覆寫 Reset() 以恢復 a DataSet 的原始狀態。

ShouldSerializeRelations()

會得到一個值,表示是否 Relations 應該保留屬性。

ShouldSerializeTables()

會得到一個值,表示是否 Tables 應該保留屬性。

ToString()

回傳 String 包含 的名稱 Component(若有的話)。 此方法不應被覆蓋。

(繼承來源 MarshalByValueComponent)
WriteXml(Stream, XmlWriteMode)

會寫入目前的資料,並可選擇性地寫入結構, DataSet 使用指定的 StreamXmlWriteMode。 要撰寫結構,將參數值設 modeWriteSchema

WriteXml(Stream)

使用指定的 Stream寫入當前資料DataSet

WriteXml(String, XmlWriteMode)

將目前的資料,以及可選的結構 DataSet ,寫入指定檔案,使用指定的 XmlWriteMode。 要撰寫結構,將參數值設 modeWriteSchema

WriteXml(String)

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

WriteXml(TextWriter, XmlWriteMode)

會寫入目前的資料,並可選擇性地寫入結構, DataSet 使用指定的 TextWriterXmlWriteMode。 要撰寫結構,將參數值設 modeWriteSchema

WriteXml(TextWriter)

使用指定的 TextWriter寫入當前資料DataSet

WriteXml(XmlWriter, XmlWriteMode)

會寫入目前的資料,並可選擇性地寫入結構, DataSet 使用指定的 XmlWriterXmlWriteMode。 要撰寫結構,將參數值設 modeWriteSchema

WriteXml(XmlWriter)

將當前 DataSet 資料寫入指定的 XmlWriter

WriteXmlSchema(Stream, Converter<Type,String>)

DataSet 結構以 XML 架構寫入指定的 Stream 物件。

WriteXmlSchema(Stream)

DataSet 結構以 XML 架構寫入指定的 Stream 物件。

WriteXmlSchema(String, Converter<Type,String>)

DataSet 結構以 XML 架構寫入檔案。

WriteXmlSchema(String)

DataSet 結構以 XML 架構寫入檔案。

WriteXmlSchema(TextWriter, Converter<Type,String>)

DataSet 結構寫成指定的 XML 結構 TextWriter

WriteXmlSchema(TextWriter)

DataSet 結構以 XML 架構寫入指定的 TextWriter 物件。

WriteXmlSchema(XmlWriter, Converter<Type,String>)

DataSet 結構寫成指定的 XML 結構 XmlWriter

WriteXmlSchema(XmlWriter)

DataSet 結構寫成 XML 架構到 XmlWriter 物件上。

事件

名稱 Description
Disposed

新增一個事件處理器來監聽元件上的事件。Disposed

(繼承來源 MarshalByValueComponent)
Initialized

發生在初始 DataSet 化之後。

MergeFailed

當目標與來源 DataRow 擁有相同的主鍵值,且 EnforceConstraints 設定為 true。

明確介面實作

名稱 Description
IListSource.ContainsListCollection

關於此成員的描述,請參見 ContainsListCollection

IListSource.GetList()

關於此成員的描述,請參見 GetList()

ISerializable.GetObjectData(SerializationInfo, StreamingContext)

填充序列化資訊物件所需的資料。DataSet

IXmlSerializable.GetSchema()

關於此成員的描述,請參見 GetSchema()

IXmlSerializable.ReadXml(XmlReader)

關於此成員的描述,請參見 ReadXml(XmlReader)

IXmlSerializable.WriteXml(XmlWriter)

關於此成員的描述,請參見 WriteXml(XmlWriter)

適用於

執行緒安全性

此類型適合多執行緒讀取操作。 您必須同步處理任何寫入作業。

另請參閱

  • 使用 ADO.NET