DataSet クラスとは? わかりやすく解説

DataSet クラス

データメモリキャッシュ表します

名前空間: System.Data
アセンブリ: System.Data (system.data.dll 内)
構文構文

<SerializableAttribute> _
Public Class DataSet
    Inherits MarshalByValueComponent
    Implements IListSource, IXmlSerializable, ISupportInitializeNotification,
 ISupportInitialize, _
    ISerializable
[SerializableAttribute] 
public class DataSet : MarshalByValueComponent,
 IListSource, IXmlSerializable, ISupportInitializeNotification, 
    ISupportInitialize, ISerializable
[SerializableAttribute] 
public ref class DataSet : public
 MarshalByValueComponent, IListSource, IXmlSerializable, ISupportInitializeNotification,
 
    ISupportInitialize, ISerializable
/** @attribute SerializableAttribute() */ 
public class DataSet extends MarshalByValueComponent
 implements IListSource, IXmlSerializable, 
    ISupportInitializeNotification, ISupportInitialize, ISerializable
SerializableAttribute 
public class DataSet extends
 MarshalByValueComponent implements IListSource, IXmlSerializable, 
    ISupportInitializeNotification, ISupportInitialize, ISerializable
解説解説

データ ソースから取得されデータメモリキャッシュである DataSet は、ADO.NET アーキテクチャの主要コンポーネントです。DataSet は、DataRelation オブジェクト相互に関連付けることができる DataTable オブジェクトコレクション構成されます。UniqueConstraint オブジェクトと ForeignKeyConstraint オブジェクト使用してDataSet 内でデータ整合性適用することもできますDataSet オブジェクト使用詳細については、「ADO.NET での 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. DataSet で AcceptChanges を呼び出します。変更キャンセルするには、RejectChanges を呼び出します。

メモメモ

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

使用例使用例

いくつかのメソッド組み合わせて DataSet作成した後で Northwind データベースデータ読み込む例を次に示します

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.Object
   System.ComponentModel.MarshalByValueComponent
    System.Data.DataSet
スレッド セーフスレッド セーフ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「DataSet クラス」の関連用語

DataSet クラスのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



DataSet クラスのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS