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
- 继承
- 属性
- 实现
示例
以下示例由几种方法组成,这些方法组合在一起,从 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
注解
有关此 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 |
获取或设置一个值,该值指示在尝试执行任何更新操作时是否遵循约束规则。 |
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)。 |
扩展方法
适用于
线程安全性
此类型对于多线程读取操作是安全的。 必须同步任何写入操作。