DataSet 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
代表資料的記憶體內部快取。
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 資料庫建立和填滿 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
備註
DataSet,這是從資料來源擷取的資料記憶體內部快取,是 ADO.NET 架構的主要元件。 DataSet是由物件集合 DataTable 所組成,您可以彼此與 DataRelation 物件建立關聯。 您也可以使用 UniqueConstraint 和 ForeignKeyConstraint 物件,在 中 DataSet 強制執行資料完整性。 如需使用 DataSet 物件的詳細資訊,請參閱 DataSets、DataTables 和 DataViews。
而 DataTable 物件則包含資料, DataRelationCollection 可讓您流覽資料表階層。 資料表包含在 DataTableCollection 透過 屬性存取的 Tables 中。 存取 DataTable 物件時,請注意它們會有條件區分大小寫。 例如,如果其中一個 DataTable 名稱為 「mydatatable」,另一個名為 「Mydatatable」,則用來搜尋其中一個資料表的字串會被視為區分大小寫。 不過,如果 「mydatatable」 存在且 「Mydatatable」 不存在,則搜尋字串會被視為不區分大小寫。 如需使用 DataTable 物件的詳細資訊,請參閱 建立 DataTable。
DataSet可以讀取和寫入資料和架構做為 XML 檔。 然後,資料與架構可以跨 HTTP 傳輸,並供任何啟用 XML 之平臺上的應用程式使用。 您可以使用 方法將架構儲存為 XML 架構 WriteXmlSchema ,而且可以使用 方法來儲存 WriteXml 架構和資料。 若要讀取包含架構和資料的 XML 檔,請使用 ReadXml 方法。
在典型的多層式實作中,建立和重新 DataSet 整理 的步驟會接著更新原始資料:
使用 DataAdapter 建置每個 DataTable ,並以資料來源中的資料填入 DataSet 。
叫用 GetChanges 方法以建立第二 DataSet 個只包含資料變更的功能。
Update呼叫 的 DataAdapter 方法,傳遞第二 DataSet 個做為引數。
AcceptChanges在 上叫用 DataSet 。 或者,叫 RejectChanges 用 以取消變更。
注意
DataSet和 DataTable 物件繼承自 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 |
取得或設定值,指出在嘗試任何更新作業時,是否遵循條件約束 (Constraint) 規則。 |
Events |
取得附加在這個元件上的事件處理常式清單。 (繼承來源 MarshalByValueComponent) |
ExtendedProperties |
取得與 |
HasErrors | |
IsInitialized |
取得值,指出 DataSet 是否已初始化。 |
Locale |
取得或設定用來在資料表中比較字串的地區設定 (Locale) 資訊。 |
Namespace |
取得或設定 DataSet 的命名空間。 |
Prefix |
取得或設定建立 DataSet 命名空間別名的 XML 前置詞。 |
Relations |
取得關聯的集合,這些關聯會連結資料表,並允許從父資料表巡覽至子資料表。 |
RemotingFormat |
取得或設定遠端處理期間所使用 之 的序列化格式 DataSet 。 |
SchemaSerializationMode |
取得或設定 SchemaSerializationMode 的 DataSet。 |
Site | |
Tables |
取得包含在 DataSet 中的資料表的集合。 |
方法
事件
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)。 |
適用於
執行緒安全性
此類型適用于多執行緒讀取作業。 您必須同步處理任何寫入作業。