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 数据库中的 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
注解
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。
A DataSet 可以读取和写入 XML 文档的数据和架构。 然后,可以在启用 XML 的任何平台上跨 HTTP 传输数据和架构,并由任何应用程序使用。 可以使用该方法将架构另存为 XML 架构 WriteXmlSchema ,并且可以使用该方法保存 WriteXml 架构和数据。 若要读取包含架构和数据的 XML 文档,请使用 ReadXml 该方法。
在典型的多层实现中,创建和刷新原始 DataSet数据的步骤反过来会更新原始数据:
Build and fill each DataTable in a DataSet with data from a data source using a DataAdapter.
调用该方法 GetChanges 以创建一个仅功能对数据所做的更改的第二个 DataSet 方法。
Update DataAdapter调用该方法,将第二DataSet个作为参数传递。
AcceptChanges调用 on the DataSet. 或者,调用 RejectChanges 以取消更改。
备注
和DataSetDataTable对象继承自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 |
获取或设置一个值,该值指示在尝试执行任何更新操作时是否遵循约束规则。 |
Events |
获取附加到该组件的事件处理程序的列表。 (继承自 MarshalByValueComponent) |
ExtendedProperties |
获取与 |
HasErrors | |
IsInitialized |
获取一个值,该值指示是否已初始化 DataSet。 |
Locale |
获取或设置用于比较表中字符串的区域设置信息。 |
Namespace |
获取或设置 DataSet 的命名空间。 |
Prefix |
获取或设置一个 XML 前缀,该前缀是 DataSet 的命名空间的别名。 |
Relations |
获取用于将表链接起来并允许从父表浏览到子表的关系的集合。 |
RemotingFormat |
获取或设置远程处理期间使用的序列化格式 DataSet 。 |
SchemaSerializationMode |
获取或设置 SchemaSerializationMode 的 DataSet。 |
Site | |
Tables |
获取包含在 DataSet 中的表的集合。 |
方法
事件
Disposed |
添加用于侦听组件的 Disposed 事件的事件处理程序。 (继承自 MarshalByValueComponent) |
Initialized |
初始化 DataSet 后发生。 |
MergeFailed |
当目标和源 DataRow 的主键值相同且 EnforceConstraints 设置为真时发生。 |
显式接口实现
IListSource.ContainsListCollection |
有关此成员的说明,请参见 ContainsListCollection。 |
IListSource.GetList() |
有关此成员的说明,请参见 GetList()。 |
ISerializable.GetObjectData(SerializationInfo, StreamingContext) |
使用序列化 DataSet 时所需的数据填充序列化信息对象。 |
IXmlSerializable.GetSchema() |
有关此成员的说明,请参见 GetSchema()。 |
IXmlSerializable.ReadXml(XmlReader) |
有关此成员的说明,请参见 ReadXml(XmlReader)。 |
IXmlSerializable.WriteXml(XmlWriter) |
有关此成员的说明,请参见 WriteXml(XmlWriter)。 |
适用于
线程安全性
此类型对于多线程读取操作是安全的。 必须同步任何写入操作。