Represents an in-memory cache of data.
Assembly: System.Data (in System.Data.dll)
<SerializableAttribute> _ Public Class DataSet _ Inherits MarshalByValueComponent _ Implements IListSource, IXmlSerializable, ISupportInitializeNotification, ISupportInitialize, _ ISerializable
Dim instance As DataSet
[SerializableAttribute] public class DataSet : MarshalByValueComponent, IListSource, IXmlSerializable, ISupportInitializeNotification, ISupportInitialize, ISerializable
[SerializableAttribute] public ref class DataSet : public MarshalByValueComponent, IListSource, IXmlSerializable, ISupportInitializeNotification, ISupportInitialize, ISerializable
public class DataSet extends MarshalByValueComponent implements IListSource, IXmlSerializable, ISupportInitializeNotification, ISupportInitialize, ISerializable
The DataSet, which is an in-memory cache of data retrieved from a data source, is a major component of the ADO.NET architecture. The DataSet consists of a collection of DataTable objects that you can relate to each other with DataRelation objects. You can also enforce data integrity in the DataSet by using the UniqueConstraint and ForeignKeyConstraint objects. For further details about working with DataSet objects, see DataSets, DataTables, and DataViews (ADO.NET).
Whereas DataTable objects contain the data, the DataRelationCollection allows you to navigate though the table hierarchy. The tables are contained in a DataTableCollection accessed through the Tables property. When accessing DataTable objects, note that they are conditionally case sensitive. For example, if one DataTable is named "mydatatable" and another is named "Mydatatable", a string used to search for one of the tables is regarded as case sensitive. However, if "mydatatable" exists and "Mydatatable" does not, the search string is regarded as case insensitive. For more information about working with DataTable objects, see Creating a DataTable (ADO.NET).
A DataSet can read and write data and schema as XML documents. The data and schema can then be transported across HTTP and used by any application, on any platform that is XML-enabled. You can save the schema as an XML schema with the WriteXmlSchema method, and both schema and data can be saved using the WriteXml method. To read an XML document that includes both schema and data, use the ReadXml method.
In a typical multiple-tier implementation, the steps for creating and refreshing a DataSet, and in turn, updating the original data are to:
-
Build and fill each DataTable in a DataSet with data from a data source using a DataAdapter.
-
Change the data in individual DataTable objects by adding, updating, or deleting DataRow objects.
-
Invoke the GetChanges method to create a second DataSet that features only the changes to the data.
-
Call the Update method of the DataAdapter, passing the second DataSet as an argument.
-
Invoke the Merge method to merge the changes from the second DataSet into the first.
-
Invoke the AcceptChanges on the DataSet. Alternatively, invoke RejectChanges to cancel the changes.
Note:
|
|---|
|
The DataSet and DataTable objects inherit from MarshalByValueComponent, and support the ISerializable interface for remoting. These are the only ADO.NET objects that can be remoted. |
Note:
|
|---|
|
Classes inherited from DataSet are not finalized by the garbage collector, because the finalizer has been suppressed in DataSet. The derived class can call the ReRegisterForFinalize method in its constructor to allow the class to be finalized by the garbage collector. |
The following example consists of several methods that, combined, create and fill a DataSet from the Northwind database.
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 SqlCommand = 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 DataSet = 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 SqlDataAdapter = _ New SqlDataAdapter() productsAdapter.TableMappings.Add("Table", "Products") Dim productsCommand As SqlCommand = 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 DataRelation = New _ System.Data.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
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"; } } }
System.ComponentModel.MarshalByValueComponent
System.Data.DataSet
This type is safe for multithreaded read operations. You must synchronize any write operations.
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
.NET Framework
Supported in: 3.5, 3.0, 2.0, 1.1, 1.0.NET Compact Framework
Supported in: 3.5, 2.0, 1.0XNA Framework
Supported in: 3.0, 2.0, 1.0Reference
Other Resources
This example shows how to use DotNetZip to save the XML representation of a dataset into a .ZIP file, using an anonymous delegate. The DataSet XML is never saved to a filesystem file.
using (var c1= new System.Data.SqlClient.SqlConnection(connstring1))
{
var da = new System.Data.SqlClient.SqlDataAdapter()
{
SelectCommand= new System.Data.SqlClient.SqlCommand(strSelect, c1)
};
DataSet ds1 = new DataSet();
da.Fill(ds1, "Invoices");
using(Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile())
{
zip.AddEntry(zipEntryName, (name,stream) => ds1.WriteXml(stream) );
zip.Save(zipFileName);
}
}
This example requires DotNetZip - http://dotnetzip.codeplex.com
Note: