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
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicConstructors | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
public class DataSet : System.ComponentModel.MarshalByValueComponent, System.ComponentModel.IListSource, System.ComponentModel.ISupportInitialize, 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
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicConstructors | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)>]
type DataSet = class
inherit MarshalByValueComponent
interface IListSource
interface ISupportInitialize
interface ISupportInitializeNotification
interface ISerializable
interface 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
- 继承
- 属性
- 实现
示例
以下示例包含几种方法,这些方法组合、创建和填充了 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 对象将它们彼此关联。 您还可以在DataSet中通过使用UniqueConstraint和ForeignKeyConstraint对象来实施数据完整性。 有关使用 DataSet 对象的更多详细信息,请参阅 DataSets、DataTables 和 DataViews。
而 DataTable 对象包含数据, DataRelationCollection 则允许你通过表层次结构进行导航。 表格包含在 DataTableCollection 中,并通过 Tables 属性访问。 访问 DataTable 对象时,请注意它们是有条件的区分大小写的。 例如,如果一个 DataTable 名为“mydatatable”,另一个名为“Mydatatable”,则用于搜索其中一个表的字符串被认为区分大小写。 但是,如果“mydatatable”存在且“Mydatatable”不存在,则搜索字符串被视为不区分大小写。 有关使用 DataTable 对象的详细信息,请参阅 创建 DataTable。
DataSet 可以以 XML 文档的形式读取和写入数据和模式。 然后,可以在启用 XML 的任何平台上跨 HTTP 传输数据和架构,并由任何应用程序使用。 可以使用该方法将架构另存为 XML 架构 WriteXmlSchema ,并且可以使用该方法保存 WriteXml 架构和数据。 若要读取包含架构和数据的 XML 文档,请使用 ReadXml 该方法。
在典型的多层实现中,创建和刷新原始 DataSet数据的步骤依次是:
使用 DataTable 构建 DataSet 中的每个 DataAdapter,并用来自数据源的数据对其进行填充。
调用 GetChanges 方法以创建仅包含对数据的更改的第二个 DataSet。
调用 Update 的 DataAdapter 方法,并将第二个 DataSet 作为参数传递。
在AcceptChanges上调用DataSet 或者,调用 RejectChanges 以取消更改。
注释
DataSet和DataTable对象继承自MarshalByValueComponent,并支持用于远程处理的ISerializable接口。 这些是唯一可以远程处理的 ADO.NET 对象。
注释
垃圾收集器不会最终确定从 DataSet 中继承的类,因为已经在 DataSet 中抑制了终结器。 派生类可以调用 ReRegisterForFinalize 其构造函数中的方法,以允许垃圾回收器完成该类。
安全注意事项
有关 DataSet 和 DataTable 安全性的信息,请参阅 安全指南。
构造函数
| 名称 | 说明 |
|---|---|
| DataSet() |
初始化 DataSet 类的新实例。 |
| DataSet(SerializationInfo, StreamingContext, Boolean) |
已过时.
使用序列化的数据初始化 DataSet 类的新实例。 |
| DataSet(SerializationInfo, StreamingContext) |
已过时.
使用序列化的数据初始化 DataSet 类的新实例。 |
| DataSet(String) |
使用给定名称初始化类的新实例 DataSet 。 |
属性
| 名称 | 说明 |
|---|---|
| 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 |
获取或设置一个 SchemaSerializationModeDataSet. |
| Site | |
| Tables |
获取包含在 |
方法
活动
| 名称 | 说明 |
|---|---|
| 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)。 |
扩展方法
适用于
线程安全性
此类型对于多线程读取操作是安全的。 必须同步任何写入操作。