Esporta (0) Stampa
Espandi tutto
EN
Il contenuto non è disponibile nella lingua di destinazione, ma solo in inglese.

DataSet Class

Represents an in-memory cache of data.

For a list of all members of this type, see DataSet Members.

System.Object
   System.ComponentModel.MarshalByValueComponent
      System.Data.DataSet

[Visual Basic]
<Serializable>
Public Class DataSet
   Inherits MarshalByValueComponent
   Implements IListSource, ISupportInitialize, ISerializable
[C#]
[Serializable]
public class DataSet : MarshalByValueComponent, IListSource,
   ISupportInitialize, ISerializable
[C++]
[Serializable]
public __gc class DataSet : public MarshalByValueComponent,
   IListSource, ISupportInitialize, ISerializable
[JScript]
public
   Serializable
class DataSet extends MarshalByValueComponent implements
   IListSource, ISupportInitialize, ISerializable

Thread Safety

This type is safe for multithreaded read operations. You must synchronize any write operations.

Remarks

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 Creating and Using DataSets.

While 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.

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:

  1. Build and fill each DataTable in a DataSet with data from a data source using a DataAdapter.
  2. Change the data in individual DataTable objects by adding, updating, or deleting DataRow objects.
  3. Invoke the GetChanges method to create a second DataSet that features only the changes to the data.
  4. Call the Update method of the DataAdapter, passing the second DataSet as an argument.
  5. Invoke the Merge method to merge the changes from the second DataSet into the first.
  6. 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.

Example

[Visual Basic, C#, C++] The following example consists of several methods that, combined, create and fill a DataSet from the Northwind database installed as a sample database with SQLServer 7.0.

[Visual Basic] 
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Drawing
Imports System.ComponentModel
Imports System.Windows.Forms

public class DataGridSample
   Inherits Form
   Private  ds As DataSet 
   Private myGrid As DataGrid 

   Shared Sub Main
      Application.Run(new DataGridSample())
   End Sub

   public Sub New()
      InitializeComponent()
   End Sub

   public Sub InitializeComponent()
      Me.ClientSize = new Size(550, 450)
      myGrid = new DataGrid()
      myGrid.Location = new Point (10,10)
      myGrid.Size = new Size(500, 400)
      myGrid.CaptionText = "Microsoft .NET DataGrid"
      Me.Controls.Add(myGrid)
      Me.Text = "Visual Basic Grid Example"         
      ConnectToData()
      myGrid.SetDataBinding(ds, "Suppliers")
   End Sub

   Private Sub ConnectToData()
      ' Create the ConnectionString and create a SqlConnection.
      ' Change the data source value to the name of your computer.
      Dim cString As string = "Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=mySQLServer"
      Dim cnNorthwind As SqlConnection  = new SqlConnection(cString)
      ' Create a SqlDataAdapter for the Suppliers table.
      Dim adpSuppliers As SqlDataAdapter = new SqlDataAdapter()
      ' A table mapping tells the adapter what to call the table.
      adpSuppliers.TableMappings.Add("Table", "Suppliers")
      cnNorthwind.Open()
      Dim cmdSuppliers As SqlCommand = _
      new SqlCommand("SELECT * FROM Suppliers", cnNorthwind)
      cmdSuppliers.CommandType = CommandType.Text

      adpSuppliers.SelectCommand = cmdSuppliers
      Console.WriteLine("The connection is open.")
      ds = New DataSet("Customers")
      adpSuppliers.Fill(ds)
      ' Create a second SqlDataAdapter and SqlCommand to get
      ' the Products table, a child table of Suppliers. 
      Dim adpProducts As SqlDataAdapter = new SqlDataAdapter()
      adpProducts.TableMappings.Add("Table", "Products")
      Dim cmdProducts As SqlCommand = _
      new SqlCommand("SELECT * FROM Products", cnNorthwind)
      adpProducts.SelectCommand = cmdProducts
      adpProducts.Fill(ds)
      cnNorthwind.Close()
      Console.WriteLine("The connection is closed.")
      ' You must create a DataRelation to link the two tables.
      Dim  dr As DataRelation     
      Dim  dc1 As DataColumn     
      Dim dc2 As DataColumn 
      ' Get the parent and child columns of the two tables.
      dc1 = ds.Tables("Suppliers").Columns("SupplierID")
      dc2 = ds.Tables("Products").Columns("SupplierID")
      dr = new System.Data.DataRelation("suppliers2products", dc1, dc2)
      ds.Relations.Add(dr)
   End Sub
End Class

[C#] 
using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;

public class DataGridSample:Form{
   DataSet ds;
   DataGrid myGrid;

   static void Main(){
      Application.Run(new DataGridSample());
   }

   public DataGridSample(){
      InitializeComponent();
   }

   void InitializeComponent(){
      this.ClientSize = new System.Drawing.Size(550, 450);
      myGrid = new DataGrid();
      myGrid.Location = new Point (10,10);
      myGrid.Size = new Size(500, 400);
      myGrid.CaptionText = "Microsoft .NET DataGrid";
      this.Text = "C# Grid Example";
      this.Controls.Add(myGrid);
      ConnectToData();
      myGrid.SetDataBinding(ds, "Suppliers");
   }
   void ConnectToData(){
      // Create the ConnectionString and create a SqlConnection.
      // Change the data source value to the name of your computer.
      
      string cString = "Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=mySQLServer";
      SqlConnection myConnection = new SqlConnection(cString);
      // Create a SqlDataAdapter.
      SqlDataAdapter myAdapter = new SqlDataAdapter();
      myAdapter.TableMappings.Add("Table", "Suppliers");
      myConnection.Open();
      SqlCommand myCommand = new SqlCommand("SELECT * FROM Suppliers",
      myConnection);
      myCommand.CommandType = CommandType.Text;
   
      myAdapter.SelectCommand = myCommand;
      Console.WriteLine("The connection is open");
      ds = new DataSet("Customers");
      myAdapter.Fill(ds);
      // Create a second Adapter and Command.
      SqlDataAdapter adpProducts = new SqlDataAdapter();
      adpProducts.TableMappings.Add("Table", "Products");
      SqlCommand cmdProducts = new SqlCommand("SELECT * FROM Products", 
      myConnection);
      adpProducts.SelectCommand = cmdProducts;
      adpProducts.Fill(ds);
      myConnection.Close();
      Console.WriteLine("The connection is closed.");
      System.Data.DataRelation dr;
      System.Data.DataColumn dc1;
      System.Data.DataColumn dc2;
      // Get the parent and child columns of the two tables.
      dc1 = ds.Tables["Suppliers"].Columns["SupplierID"];
      dc2 = ds.Tables["Products"].Columns["SupplierID"];
      dr = new System.Data.DataRelation("suppliers2products", dc1, dc2);
      ds.Relations.Add(dr);
   }
}

[C++] 
#using <mscorlib.dll>
#using <System.Xml.dll>
#using <System.dll>
#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>
#using <System.Data.dll>
using namespace System;
using namespace System::Data;
using namespace System::Data::SqlClient;
using namespace System::Drawing;
using namespace System::Windows::Forms;

public __gc class DataGridSample:public Form{
   DataSet* ds;
   DataGrid* myGrid;

public:
   DataGridSample(){
      InitializeComponent();
   }

   void InitializeComponent(){
      this->ClientSize = System::Drawing::Size(550, 450);
      myGrid = new DataGrid();
      myGrid->Location = Point (10,10);
      myGrid->Size = System::Drawing::Size(500, 400);
      myGrid->CaptionText = S"Microsoft .NET DataGrid";
      this->Text = S"C# Grid Example";
      this->Controls->Add(myGrid);
      ConnectToData();
      myGrid->SetDataBinding(ds, S"Suppliers");
   }
   void ConnectToData(){
      // Create the ConnectionString and create a SqlConnection.
      // Change the data source value to the name of your computer.
      
      String* cString = S"Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=mySQLServer";
      SqlConnection* myConnection = new SqlConnection(cString);
      // Create a SqlDataAdapter.
      SqlDataAdapter* myAdapter = new SqlDataAdapter();
      myAdapter->TableMappings->Add(S"Table", S"Suppliers");
      myConnection->Open();
      SqlCommand* myCommand = new SqlCommand(S"SELECT * FROM Suppliers",
      myConnection);
      myCommand->CommandType = CommandType::Text;
   
      myAdapter->SelectCommand = myCommand;
      Console::WriteLine(S"The connection is open");
      ds = new DataSet(S"Customers");
      myAdapter->Fill(ds);
      // Create a second Adapter and Command.
      SqlDataAdapter* adpProducts = new SqlDataAdapter();
      adpProducts->TableMappings->Add(S"Table", S"Products");
      SqlCommand* cmdProducts = new SqlCommand(S"SELECT * FROM Products", 
      myConnection);
      adpProducts->SelectCommand = cmdProducts;
      adpProducts->Fill(ds);
      myConnection->Close();
      Console::WriteLine(S"The connection is closed.");
      System::Data::DataRelation* dr;
      System::Data::DataColumn* dc1;
      System::Data::DataColumn* dc2;
      // Get the parent and child columns of the two tables.
      dc1 = ds->Tables->Item[S"Suppliers"]->Columns->Item[S"SupplierID"];
      dc2 = ds->Tables->Item[S"Products"]->Columns->Item[S"SupplierID"];
      dr = new System::Data::DataRelation(S"suppliers2products", dc1, dc2);
      ds->Relations->Add(dr);
   }
};

int main(){
   Application::Run(new DataGridSample());
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Data

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: System.Data (in System.Data.dll)

See Also

DataSet Members | System.Data Namespace | OleDbDataAdapter | DataTable | DataRelation | DataRow | DataView | SqlDataAdapter | ForeignKeyConstraint | UniqueConstraint

Microsoft sta conducendo un sondaggio in linea per comprendere l'opinione degli utenti in merito al sito Web di MSDN. Se si sceglie di partecipare, quando si lascia il sito Web di MSDN verrà visualizzato il sondaggio in linea.

Si desidera partecipare?
Mostra:
© 2014 Microsoft