DataSet 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
代表記憶體中的資料快取。
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 |
取得與 |
| HasErrors | |
| IsInitialized |
會得到一個表示是否 DataSet 已初始化的值。 |
| Locale |
取得或設定用於比較表格中字串的地點資訊。 |
| Namespace |
取得或設定 的 DataSet命名空間。 |
| Prefix |
取得或設定一個 XML 前綴,使 的命名空間 DataSet別名化。 |
| Relations |
取得連結資料表並允許從父資料表到子資料表的關聯集合。 |
| RemotingFormat |
它會取得或設定在遠端執行時使用的序列化格式 DataSet 。 |
| SchemaSerializationMode |
取得或設定 a SchemaSerializationMode 為 DataSet。 |
| Site | |
| Tables |
取得包含在 DataSet的表格集合。 |
方法
事件
| 名稱 | 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