DataSet クラス

データのメモリ内キャッシュを表します。

この型のすべてのメンバの一覧については、DataSet メンバ を参照してください。

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

<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

スレッドセーフ

この型は、マルチスレッド読み取り操作に対して安全です。すべての書き込み操作の同期をとる必要があります。

解説

データ ソースから取得されたデータのメモリ内キャッシュである DataSet は、ADO.NET アーキテクチャの主要コンポーネントです。 DataSet は、 DataRelation オブジェクトと相互に関連付けることができる DataTable オブジェクトのコレクションで構成されます。 UniqueConstraint オブジェクトと ForeignKeyConstraint オブジェクトを使用して、 DataSet 内でデータの整合性を適用することもできます。 DataSet オブジェクトの使用の詳細については、「 DataSet の作成および使用 」を参照してください。

DataTable オブジェクトにはデータを格納できるのに対して、 DataRelationCollection を使用するとテーブルの階層構造内を移動できます。テーブルは、 Tables プロパティを使用してアクセスできる DataTableCollection に格納されます。 DataTable オブジェクトにアクセスするときは、条件付きで大文字と小文字が区別されることに注意してください。たとえば、"mydatatable" という名前の DataTable と "Mydatatable" という名前のテーブルがある場合は、この 2 つのーブルのどちらかを検索する文字列は大文字と小文字を区別すると見なされます。ただし、"mydatatable" という名前は存在するが "Mydatatable" という名前が存在しない場合は、検索文字列は大文字と小文字を区別しないと見なされます。 DataTable オブジェクトの使用の詳細については、「 DataTable の作成 」を参照してください。

DataSet では、データとスキーマを XML ドキュメントとして読み取ったり、書き込んだりできます。読み込んだデータとスキーマは、HTTP で転送でき、XML 対応のすべてのプラットフォームおよびアプリケーションで使用できます。スキーマを XML スキーマとして保存するには WriteXmlSchema メソッドを使用します。スキーマとデータの両方を保存するには WriteXml メソッドを使用します。スキーマとデータの両方を含む XML ドキュメントを読み取るには、 ReadXml メソッドを使用します。

通常の多階層の実装で DataSet を作成および更新し、次に元のデータを更新するステップを次に示します。

  1. DataAdapter を使用して、 DataSet 内に DataTable を作成し、各テーブルにデータ ソースのデータを格納します。
  2. DataRow オブジェクトを追加、更新、または削除して、個別の DataTable オブジェクト内のデータを変更します。
  3. GetChanges メソッドを呼び出して、データへの変更だけを格納する 2 つ目の DataSet を作成します。
  4. この 2 つ目の DataSet を引数として渡して、 DataAdapterUpdate メソッドを呼び出します。
  5. Merge を呼び出して、2 つ目の DataSet に格納された変更を最初のデータセットにマージします。
  6. DataSetAcceptChanges を呼び出します。変更をキャンセルするには、 RejectChanges を呼び出します。

メモ    DataSet オブジェクトと DataTable オブジェクトは MarshalByValueComponent から継承し、リモート処理用の ISerializable インターフェイスをサポートします。リモート処理できる ADO.NET オブジェクトはこれらのオブジェクトだけです。

使用例

[Visual Basic, C#, C++] DataSet を作成し、SQLServer 7.0 にサンプル データベースとしてインストールされている Northwind データベースからデータを格納する、複数のメソッドの組み合わせを次の例に示します。

 
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] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

名前空間: System.Data

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

アセンブリ: System.Data (System.Data.dll 内)

参照

DataSet メンバ | System.Data 名前空間 | OleDbDataAdapter | DataTable | DataRelation | DataRow | DataView | SqlDataAdapter | ForeignKeyConstraint | UniqueConstraint