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
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
- 繼承
- 屬性
- 實作
範例
下列範例包含數種方法,這些方法結合、建立及填滿 DataSetNorthwind 資料庫中的 。
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 |
取得與 |
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)。 |
擴充方法
適用於
執行緒安全性
此類型適用於多線程讀取作業。 您必須同步處理任何寫入作業。