次の方法で共有


DataSet.Load メソッド

定義

指定したIDataReaderを使用して、データ ソースの値をDataSetに入力します。

オーバーロード

名前 説明
Load(IDataReader, LoadOption, DataTable[])

指定したIDataReaderを使用し、スキーマと名前空間の情報を提供するためにDataTableインスタンスの配列を使用して、データ ソースの値をDataSetに入力します。

Load(IDataReader, LoadOption, String[])

指定したIDataReaderを使用し、文字列の配列を使用してデータ ソースの値をDataSetに入力し、DataSet内のテーブルの名前を指定します。

Load(IDataReader, LoadOption, FillErrorEventHandler, DataTable[])

指定したIDataReaderを使用し、スキーマと名前空間の情報を提供するためにDataTableインスタンスの配列を使用して、データ ソースの値をDataSetに入力します。

注釈

Load メソッドは、単一のDataTableにデータを入力する手法を提供し、IDataReader インスタンスから取得します。 このメソッドは同じ機能を提供しますが、DataSet内の複数のテーブルにIDataReaderから複数の結果セットを読み込むことができます。

DataSetに既に行が含まれている場合、データ ソースからの受信データは既存の行とマージされます。

Load メソッドは、指定したデータ ソースからデータを取得し、現在のデータ コンテナー (この場合はDataSet) に追加することを中心に、いくつかの一般的なシナリオで使用できます。 これらのシナリオでは、 DataSetの標準的な使用方法について説明し、その更新とマージの動作について説明します。

DataSetは、単一のプライマリ データ ソースと同期または更新します。 DataSetは変更を追跡し、プライマリ データ ソースとの同期を可能にします。 さらに、 DataSet は、1 つ以上のセカンダリ データ ソースからの増分データを受け入れることもできます。 DataSetは、セカンダリ データ ソースとの同期を許可するために変更を追跡する責任を負いません。

これら 2 つの架空のデータ ソースを考えると、ユーザーは次のいずれかの動作を必要とする可能性があります。

  • プライマリ データ ソースから DataSet を初期化します。 このシナリオでは、ユーザーはプライマリ データ ソースの値を使用して空の DataSet を初期化する必要があります。 1 つ以上の DataTable の内容が変更されます。 その後、ユーザーは変更をプライマリ データ ソースに反映する予定です。

  • 変更を保持し、プライマリ データ ソースから再同期します。 このシナリオでは、ユーザーは前のシナリオで入力した DataSet を取得し、プライマリ データ ソースとの増分同期を実行し、 DataSetで行われた変更を保持したいと考えています。

  • セカンダリ データ ソースからの増分データ フィード。 このシナリオでは、ユーザーは 1 つ以上のセカンダリ データ ソースからの変更をマージし、それらの変更をプライマリ データ ソースに反映したいと考えています。

Loadメソッドを使用すると、これらすべてのシナリオが可能になります。 このメソッドを使用すると、読み込みオプション パラメーターを指定して、 DataTable 内の行が読み込まれる行と結合する方法を示すことができます。 次の表では、 LoadOption 列挙体によって提供される 3 つの読み込みオプションについて説明します。 いずれの場合も、説明は、受信データ内の行の主キーが既存の行の主キーと一致する場合の動作を示します。

読み込みオプション 説明
PreserveChanges (既定値) 元のバージョンの行を受信行の値で更新します。
OverwriteChanges 行の現在のバージョンと元のバージョンを、受信行の値で更新します。
Upsert 現在のバージョンの行を受信行の値で更新します。

一般に、 PreserveChanges オプションと OverwriteChanges オプションは、ユーザーが DataSet とその変更をプライマリ データ ソースと同期する必要があるシナリオを対象としています。 Upsert オプションを使用すると、1 つ以上のセカンダリ データ ソースからの変更の集計が容易になります。

Load(IDataReader, LoadOption, DataTable[])

ソース:
DataSet.cs
ソース:
DataSet.cs
ソース:
DataSet.cs
ソース:
DataSet.cs
ソース:
DataSet.cs

指定したIDataReaderを使用し、スキーマと名前空間の情報を提供するためにDataTableインスタンスの配列を使用して、データ ソースの値をDataSetに入力します。

public:
 void Load(System::Data::IDataReader ^ reader, System::Data::LoadOption loadOption, ... cli::array <System::Data::DataTable ^> ^ tables);
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Using LoadOption may cause members from types used in the expression column to be trimmed if not referenced directly.")]
public void Load(System.Data.IDataReader reader, System.Data.LoadOption loadOption, params System.Data.DataTable[] tables);
public void Load(System.Data.IDataReader reader, System.Data.LoadOption loadOption, params System.Data.DataTable[] tables);
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Using LoadOption may cause members from types used in the expression column to be trimmed if not referenced directly.")>]
member this.Load : System.Data.IDataReader * System.Data.LoadOption * System.Data.DataTable[] -> unit
member this.Load : System.Data.IDataReader * System.Data.LoadOption * System.Data.DataTable[] -> unit
Public Sub Load (reader As IDataReader, loadOption As LoadOption, ParamArray tables As DataTable())

パラメーター

reader
IDataReader

1 つ以上の結果セットを提供する IDataReader

loadOption
LoadOption

DataSet内のDataTable インスタンスに既に存在する行を、同じ主キーを共有する受信行と結合する方法を示す、LoadOption列挙体の値。

tables
DataTable[]

Load(IDataReader, LoadOption, DataTable[]) メソッドが名前と名前空間の情報を取得するDataTable インスタンスの配列。 これらの各テーブルは、このDataSetに含まれるDataTableCollectionのメンバーである必要があります。

属性

次の例では、新しいDataSetを作成し、DataSetに 2 つのDataTable インスタンスを追加し、Load メソッドを使用してDataSetに入力し、2 つの結果セットを含むDataTableReaderからデータを取得します。 最後に、コンソール ウィンドウにテーブルの内容を表示します。

static void Main()
{
    DataSet dataSet = new DataSet();

    DataTable customerTable = new DataTable();
    DataTable productTable = new DataTable();

    // This information is cosmetic, only.
    customerTable.TableName = "Customers";
    productTable.TableName = "Products";

    // Add the tables to the DataSet:
    dataSet.Tables.Add(customerTable);
    dataSet.Tables.Add(productTable);

    // Load the data into the existing DataSet.
    DataTableReader reader = GetReader();
    dataSet.Load(reader, LoadOption.OverwriteChanges,
        customerTable, productTable);

    // Print out the contents of each table:
    foreach (DataTable table in dataSet.Tables)
    {
        PrintColumns(table);
    }

    Console.WriteLine("Press any key to continue.");
    Console.ReadKey();
}

private static DataTable GetCustomers()
{
    // Create sample Customers table.
    DataTable table = new DataTable();
    table.TableName = "Customers";

    // Create two columns, ID and Name.
    DataColumn idColumn = table.Columns.Add("ID", typeof(int));
    table.Columns.Add("Name", typeof(string));

    // Set the ID column as the primary key column.
    table.PrimaryKey = new DataColumn[] { idColumn };

    table.Rows.Add(new object[] { 0, "Mary" });
    table.Rows.Add(new object[] { 1, "Andy" });
    table.Rows.Add(new object[] { 2, "Peter" });
    table.AcceptChanges();
    return table;
}

private static DataTable GetProducts()
{
    // Create sample Products table.
    DataTable table = new DataTable();
    table.TableName = "Products";

    // Create two columns, ID and Name.
    DataColumn idColumn = table.Columns.Add("ID",
        typeof(int));
    table.Columns.Add("Name", typeof(string));

    // Set the ID column as the primary key column.
    table.PrimaryKey = new DataColumn[] { idColumn };

    table.Rows.Add(new object[] { 0, "Wireless Network Card" });
    table.Rows.Add(new object[] { 1, "Hard Drive" });
    table.Rows.Add(new object[] { 2, "Monitor" });
    table.Rows.Add(new object[] { 3, "CPU" });
    table.AcceptChanges();
    return table;
}

private static void PrintColumns(DataTable table)
{
    Console.WriteLine();
    Console.WriteLine(table.TableName);
    Console.WriteLine("=========================");
    // Loop through all the rows in the table:
    foreach (DataRow row in table.Rows)
    {
        for (int i = 0; i < table.Columns.Count; i++)
        {
            Console.Write(row[i] + " ");
        }
        Console.WriteLine();
    }
}

private static DataTableReader GetReader()
{
    // Return a DataTableReader containing multiple
    // result sets, just for the sake of this demo.
    DataSet dataSet = new DataSet();
    dataSet.Tables.Add(GetCustomers());
    dataSet.Tables.Add(GetProducts());
    return dataSet.CreateDataReader();
}
Sub Main()
    Dim dataSet As New DataSet

    Dim customerTable As New DataTable
    Dim productTable As New DataTable

    ' This information is cosmetic, only.
    customerTable.TableName = "Customers"
    productTable.TableName = "Products"

    ' Add the tables to the DataSet:
    dataSet.Tables.Add(customerTable)
    dataSet.Tables.Add(productTable)

    ' Load the data into the existing DataSet. 
    Dim reader As DataTableReader = GetReader()
    dataSet.Load(reader, LoadOption.OverwriteChanges, _
        customerTable, productTable)

    ' Print out the contents of each table:
    For Each table As DataTable In dataSet.Tables
        PrintColumns(table)
    Next

    Console.WriteLine("Press any key to continue.")
    Console.ReadKey()
End Sub

Private Function GetCustomers() As DataTable
    ' Create sample Customers table.
    Dim table As New DataTable
    table.TableName = "Customers"

    ' Create two columns, ID and Name.
    Dim idColumn As DataColumn = table.Columns.Add("ID", _
        GetType(Integer))
    table.Columns.Add("Name", GetType(String))

    ' Set the ID column as the primary key column.
    table.PrimaryKey = New DataColumn() {idColumn}

    table.Rows.Add(New Object() {0, "Mary"})
    table.Rows.Add(New Object() {1, "Andy"})
    table.Rows.Add(New Object() {2, "Peter"})
    table.AcceptChanges()
    Return table
End Function

Private Function GetProducts() As DataTable
    ' Create sample Products table, in order
    ' to demonstrate the behavior of the DataTableReader.
    Dim table As New DataTable
    table.TableName = "Products"

    ' Create two columns, ID and Name.
    Dim idColumn As DataColumn = table.Columns.Add("ID", _
        GetType(Integer))
    table.Columns.Add("Name", GetType(String))

    ' Set the ID column as the primary key column.
    table.PrimaryKey = New DataColumn() {idColumn}

    table.Rows.Add(New Object() {0, "Wireless Network Card"})
    table.Rows.Add(New Object() {1, "Hard Drive"})
    table.Rows.Add(New Object() {2, "Monitor"})
    table.Rows.Add(New Object() {3, "CPU"})
    Return table
End Function

Private Function GetReader() As DataTableReader
    ' Return a DataTableReader containing multiple
    ' result sets, just for the sake of this demo.
    Dim dataSet As New DataSet
    dataSet.Tables.Add(GetCustomers())
    dataSet.Tables.Add(GetProducts())
    Return dataSet.CreateDataReader()
End Function

Private Sub PrintColumns( _
   ByVal table As DataTable)

    Console.WriteLine()
    Console.WriteLine(table.TableName)
    Console.WriteLine("=========================")
    ' Loop through all the rows in the table.
    For Each row As DataRow In table.Rows
        For Each col As DataColumn In table.Columns
            Console.Write(row(col).ToString() & " ")
        Next
        Console.WriteLine()
    Next
End Sub

注釈

Load メソッドは、単一のDataTableにデータを入力する手法を提供し、IDataReader インスタンスから取得します。 このメソッドは同じ機能を提供しますが、DataSet内の複数のテーブルにIDataReaderから複数の結果セットを読み込むことができます。

受信readerのソース データ列のいずれかが計算列の場合、読み込み操作はInvalidOperationExceptionで失敗します。

loadOption パラメーターを使用すると、インポートしたデータを既存のデータとやり取りする方法を指定できます。また、LoadOption列挙体の任意の値を指定できます。 このパラメーターの使用方法の詳細については、 DataTableLoad メソッドのドキュメントを参照してください。

tables パラメーターを使用すると、リーダーから読み込まれる各結果セットに対応するテーブルの順序を示す、DataTable インスタンスの配列を指定できます。 Load メソッドは、指定された各DataTable インスタンスに、ソース データ リーダーの 1 つの結果セットのデータを入力します。 各結果セットの後、 Load メソッドは、結果セットがなくなったまで、リーダー内の次の結果セットに移動します。

このメソッドの名前解決スキームは、DbDataAdapter クラスの Fill メソッドと同じです。

こちらもご覧ください

適用対象

Load(IDataReader, LoadOption, String[])

ソース:
DataSet.cs
ソース:
DataSet.cs
ソース:
DataSet.cs
ソース:
DataSet.cs
ソース:
DataSet.cs

指定したIDataReaderを使用し、文字列の配列を使用してデータ ソースの値をDataSetに入力し、DataSet内のテーブルの名前を指定します。

public:
 void Load(System::Data::IDataReader ^ reader, System::Data::LoadOption loadOption, ... cli::array <System::String ^> ^ tables);
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Using LoadOption may cause members from types used in the expression column to be trimmed if not referenced directly.")]
public void Load(System.Data.IDataReader reader, System.Data.LoadOption loadOption, params string[] tables);
public void Load(System.Data.IDataReader reader, System.Data.LoadOption loadOption, params string[] tables);
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Using LoadOption may cause members from types used in the expression column to be trimmed if not referenced directly.")>]
member this.Load : System.Data.IDataReader * System.Data.LoadOption * string[] -> unit
member this.Load : System.Data.IDataReader * System.Data.LoadOption * string[] -> unit
Public Sub Load (reader As IDataReader, loadOption As LoadOption, ParamArray tables As String())

パラメーター

reader
IDataReader

1 つ以上の結果セットを提供する IDataReader

loadOption
LoadOption

DataSet内のDataTable インスタンスに既に存在する行を、同じ主キーを共有する受信行と結合する方法を示す、LoadOption列挙体の値。

tables
String[]

Load メソッドがテーブル名情報を取得する文字列の配列。

属性

次のコンソール アプリケーションの例では、最初にテーブルを作成し、Load メソッドを使用してリーダーからDataSetにデータを読み込みます。 次に、テーブルを DataSet に追加し、テーブルに DataTableReaderのデータを入力しようとします。 この例では、 Load メソッドに渡されるパラメーターは存在しないテーブル名を示しているため、 Load メソッドは、パラメーターとして渡された名前と一致する新しいテーブルを作成します。 データが読み込まれると、コンソール ウィンドウにすべてのテーブルの内容が表示されます。

static void Main()
{
    DataSet dataSet = new DataSet();

    DataTableReader reader = GetReader();

    // The tables listed as parameters for the Load method
    // should be in the same order as the tables within the IDataReader.
    dataSet.Load(reader, LoadOption.Upsert, "Customers", "Products");
    foreach (DataTable table in dataSet.Tables)
    {
        PrintColumns(table);
    }

    // Now try the example with the DataSet
    // already filled with data:
    dataSet = new DataSet();
    dataSet.Tables.Add(GetCustomers());
    dataSet.Tables.Add(GetProducts());

    // Retrieve a data reader containing changed data:
    reader = GetReader();

    // Load the data into the existing DataSet. Retrieve the order of the
    // the data in the reader from the
    // list of table names in the parameters. If you specify
    // a new table name here, the Load method will create
    // a corresponding new table.
    dataSet.Load(reader, LoadOption.Upsert,
        "NewCustomers", "Products");
    foreach (DataTable table in dataSet.Tables)
    {
        PrintColumns(table);
    }

    Console.WriteLine("Press any key to continue.");
    Console.ReadKey();
}

private static DataTable GetCustomers()
{
    // Create sample Customers table.
    DataTable table = new DataTable();
    table.TableName = "Customers";

    // Create two columns, ID and Name.
    DataColumn idColumn = table.Columns.Add("ID", typeof(int));
    table.Columns.Add("Name", typeof(string));

    // Set the ID column as the primary key column.
    table.PrimaryKey = new DataColumn[] { idColumn };

    table.Rows.Add(new object[] { 0, "Mary" });
    table.Rows.Add(new object[] { 1, "Andy" });
    table.Rows.Add(new object[] { 2, "Peter" });
    table.AcceptChanges();
    return table;
}

private static DataTable GetProducts()
{
    // Create sample Products table.
    DataTable table = new DataTable();
    table.TableName = "Products";

    // Create two columns, ID and Name.
    DataColumn idColumn = table.Columns.Add("ID", typeof(int));
    table.Columns.Add("Name", typeof(string));

    // Set the ID column as the primary key column.
    table.PrimaryKey = new DataColumn[] { idColumn };

    table.Rows.Add(new object[] { 0, "Wireless Network Card" });
    table.Rows.Add(new object[] { 1, "Hard Drive" });
    table.Rows.Add(new object[] { 2, "Monitor" });
    table.Rows.Add(new object[] { 3, "CPU" });
    table.AcceptChanges();
    return table;
}

private static void PrintColumns(DataTable table)
{
    Console.WriteLine();
    Console.WriteLine(table.TableName);
    Console.WriteLine("=========================");
    // Loop through all the rows in the table:
    foreach (DataRow row in table.Rows)
    {
        for (int i = 0; i < table.Columns.Count; i++)
        {
            Console.Write(row[i] + " ");
        }
        Console.WriteLine();
    }
}

private static DataTableReader GetReader()
{
    // Return a DataTableReader containing multiple
    // result sets, just for the sake of this demo.
    DataSet dataSet = new DataSet();
    dataSet.Tables.Add(GetCustomers());
    dataSet.Tables.Add(GetProducts());
    return dataSet.CreateDataReader();
}
Sub Main()
  Dim dataSet As New DataSet
  Dim table As DataTable

  Dim reader As DataTableReader = GetReader()

  ' The tables listed as parameters for the Load method 
  ' should be in the same order as the tables within the IDataReader.
  dataSet.Load(reader, LoadOption.Upsert, "Customers", "Products")
  For Each table In dataSet.Tables
    PrintColumns(table)
  Next

  ' Now try the example with the DataSet
  ' already filled with data:
  dataSet = New DataSet
  dataSet.Tables.Add(GetCustomers())
  dataSet.Tables.Add(GetProducts())

  ' Retrieve a data reader containing changed data:
  reader = GetReader()

  ' Load the data into the existing DataSet. Retrieve the order of the
  ' the data in the reader from the
  ' list of table names in the parameters. If you specify
  ' a new table name here, the Load method will create
  ' a corresponding new table.
  dataSet.Load(reader, LoadOption.Upsert, "NewCustomers", "Products")
  For Each table In dataSet.Tables
    PrintColumns(table)
  Next

  Console.WriteLine("Press any key to continue.")
  Console.ReadKey()
End Sub

Private Function GetCustomers() As DataTable
  ' Create sample Customers table.
  Dim table As New DataTable
  table.TableName = "Customers"

  ' Create two columns, ID and Name.
  Dim idColumn As DataColumn = table.Columns.Add("ID", GetType(Integer))
  table.Columns.Add("Name", GetType(String))

  ' Set the ID column as the primary key column.
  table.PrimaryKey = New DataColumn() {idColumn}

  table.Rows.Add(New Object() {0, "Mary"})
  table.Rows.Add(New Object() {1, "Andy"})
  table.Rows.Add(New Object() {2, "Peter"})
  table.AcceptChanges()
  Return table
End Function

Private Function GetProducts() As DataTable
  ' Create sample Products table, in order
  ' to demonstrate the behavior of the DataTableReader.
  Dim table As New DataTable
  table.TableName = "Products"

  ' Create two columns, ID and Name.
  Dim idColumn As DataColumn = table.Columns.Add("ID", GetType(Integer))
  table.Columns.Add("Name", GetType(String))

  ' Set the ID column as the primary key column.
  table.PrimaryKey = New DataColumn() {idColumn}

  table.Rows.Add(New Object() {0, "Wireless Network Card"})
  table.Rows.Add(New Object() {1, "Hard Drive"})
  table.Rows.Add(New Object() {2, "Monitor"})
  table.Rows.Add(New Object() {3, "CPU"})
  Return table
End Function

Private Function GetReader() As DataTableReader
  ' Return a DataTableReader containing multiple
  ' result sets, just for the sake of this demo.
  Dim dataSet As New DataSet
  dataSet.Tables.Add(GetCustomers())
  dataSet.Tables.Add(GetProducts())
  Return dataSet.CreateDataReader()
End Function

Private Sub PrintColumns( _
   ByVal table As DataTable)

  Console.WriteLine()
  Console.WriteLine(table.TableName)
  Console.WriteLine("=========================")
  ' Loop through all the rows in the table.
  For Each row As DataRow In table.Rows
    For Each col As DataColumn In table.Columns
      Console.Write(row(col).ToString() & " ")
    Next
    Console.WriteLine()
  Next
End Sub

注釈

Load メソッドは、単一のDataTableにデータを入力する手法を提供し、IDataReader インスタンスから取得します。 このメソッドは同じ機能を提供しますが、DataSet内の複数のテーブルにIDataReaderから複数の結果セットを読み込むことができます。

受信readerのソース データ列のいずれかが計算列の場合、読み込み操作はInvalidOperationExceptionで失敗します。

loadOption パラメーターを使用すると、インポートしたデータを既存のデータとやり取りする方法を指定できます。また、LoadOption列挙体の任意の値を指定できます。 このパラメーターの使用方法の詳細については、 Load メソッドのドキュメントを参照してください。

tables パラメーターを使用すると、リーダーから読み込まれた各結果セットに対応するテーブルの順序を示す、テーブル名の配列を指定できます。 Load メソッドは、テーブル名の配列で見つかった名前と一致するDataSet内のテーブルを順番に検索しようとします。 一致するテーブルが見つかった場合、そのテーブルは現在の結果セットの内容と共に読み込まれます。 一致するテーブルが見つからない場合は、テーブル名の配列に指定された名前を使用してテーブルが作成され、結果セットから新しいテーブルのスキーマが推論されます。 各結果セットの後、 Load メソッドは、結果セットがなくなったまで、リーダー内の次の結果セットに移動します。

DataSetに関連付けられている既定の名前空間 (存在する場合) は、新しく作成された各DataTableに関連付けられます。 このメソッドの名前解決スキームは、DbDataAdapter クラスの Fill メソッドと同じです。

こちらもご覧ください

適用対象

Load(IDataReader, LoadOption, FillErrorEventHandler, DataTable[])

ソース:
DataSet.cs
ソース:
DataSet.cs
ソース:
DataSet.cs
ソース:
DataSet.cs
ソース:
DataSet.cs

指定したIDataReaderを使用し、スキーマと名前空間の情報を提供するためにDataTableインスタンスの配列を使用して、データ ソースの値をDataSetに入力します。

public:
 virtual void Load(System::Data::IDataReader ^ reader, System::Data::LoadOption loadOption, System::Data::FillErrorEventHandler ^ errorHandler, ... cli::array <System::Data::DataTable ^> ^ tables);
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Using LoadOption may cause members from types used in the expression column to be trimmed if not referenced directly.")]
public virtual void Load(System.Data.IDataReader reader, System.Data.LoadOption loadOption, System.Data.FillErrorEventHandler? errorHandler, params System.Data.DataTable[] tables);
public virtual void Load(System.Data.IDataReader reader, System.Data.LoadOption loadOption, System.Data.FillErrorEventHandler? errorHandler, params System.Data.DataTable[] tables);
public virtual void Load(System.Data.IDataReader reader, System.Data.LoadOption loadOption, System.Data.FillErrorEventHandler errorHandler, params System.Data.DataTable[] tables);
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Using LoadOption may cause members from types used in the expression column to be trimmed if not referenced directly.")>]
abstract member Load : System.Data.IDataReader * System.Data.LoadOption * System.Data.FillErrorEventHandler * System.Data.DataTable[] -> unit
override this.Load : System.Data.IDataReader * System.Data.LoadOption * System.Data.FillErrorEventHandler * System.Data.DataTable[] -> unit
abstract member Load : System.Data.IDataReader * System.Data.LoadOption * System.Data.FillErrorEventHandler * System.Data.DataTable[] -> unit
override this.Load : System.Data.IDataReader * System.Data.LoadOption * System.Data.FillErrorEventHandler * System.Data.DataTable[] -> unit
Public Overridable Sub Load (reader As IDataReader, loadOption As LoadOption, errorHandler As FillErrorEventHandler, ParamArray tables As DataTable())

パラメーター

reader
IDataReader

1 つ以上の結果セットを提供する IDataReader

loadOption
LoadOption

DataSet内のDataTable インスタンスに既に存在する行を、同じ主キーを共有する受信行と結合する方法を示す、LoadOption列挙体の値。

errorHandler
FillErrorEventHandler

データの読み込み中にエラーが発生したときに呼び出す FillErrorEventHandler デリゲート。

tables
DataTable[]

Load(IDataReader, LoadOption, FillErrorEventHandler, DataTable[]) メソッドが名前と名前空間の情報を取得するDataTable インスタンスの配列。

属性

次の例では、テーブルを DataSetに追加し、 Load メソッドを使用して、互換性のないスキーマを含む DataTableReader からデータを読み込もうとします。 この例では、エラーをトラップするのではなく、 FillErrorEventHandler デリゲートを使用してエラーを調査して処理します。 出力がコンソール ウィンドウに表示されます。

static void Main()
{
    // Attempt to load data from a data reader in which
    // the schema is incompatible with the current schema.
    // If you use exception handling, you won't get the chance
    // to examine each row, and each individual table,
    // as the Load method progresses.
    // By taking advantage of the FillErrorEventHandler delegate,
    // you can interact with the Load process as an error occurs,
    // attempting to fix the problem, or simply continuing or quitting
    // the Load process.:
    DataSet dataSet = new DataSet();
    DataTable table = GetIntegerTable();
    dataSet.Tables.Add(table);
    DataTableReader reader = new DataTableReader(GetStringTable());
    dataSet.Load(reader, LoadOption.OverwriteChanges,
        FillErrorHandler, table);

    Console.WriteLine("Press any key to continue.");
    Console.ReadKey();
}

private static DataTable GetIntegerTable()
{
    // Create sample Customers table, in order
    // to demonstrate the behavior of the DataTableReader.
    DataTable table = new DataTable();

    // Create two columns, ID and Name.
    DataColumn idColumn = table.Columns.Add("ID", typeof(int));

    // Set the ID column as the primary key column.
    table.PrimaryKey = new DataColumn[] { idColumn };

    table.Rows.Add(new object[] { 4 });
    table.Rows.Add(new object[] { 5 });
    table.AcceptChanges();
    return table;
}

private static DataTable GetStringTable()
{
    // Create sample Customers table, in order
    // to demonstrate the behavior of the DataTableReader.
    DataTable table = new DataTable();

    // Create two columns, ID and Name.
    DataColumn idColumn = table.Columns.Add("ID", typeof(string));

    // Set the ID column as the primary key column.
    table.PrimaryKey = new DataColumn[] { idColumn };

    table.Rows.Add(new object[] { "Mary" });
    table.Rows.Add(new object[] { "Andy" });
    table.Rows.Add(new object[] { "Peter" });
    table.AcceptChanges();
    return table;
}

static void FillErrorHandler(object sender, FillErrorEventArgs e)
{
    // You can use the e.Errors value to determine exactly what
    // went wrong.
    if (e.Errors.GetType() == typeof(System.FormatException))
    {
        Console.WriteLine("Error when attempting to update the value: {0}",
            e.Values[0]);
    }

    // Setting e.Continue to True tells the Load
    // method to continue trying. Setting it to False
    // indicates that an error has occurred, and the
    // Load method raises the exception that got
    // you here.
    e.Continue = true;
}
Sub Main()
  Dim dataSet As New DataSet
  Dim table As New DataTable()

  ' Attempt to load data from a data reader in which
  ' the schema is incompatible with the current schema.
  ' If you use exception handling, you won't get the chance
  ' to examine each row, and each individual table,
  ' as the Load method progresses.
  ' By taking advantage of the FillErrorEventHandler delegate,
  ' you can interact with the Load process as an error occurs,
  ' attempting to fix the problem, or simply continuing or quitting
  ' the Load process.:
  dataSet = New DataSet()
  table = GetIntegerTable()
  dataSet.Tables.Add(table)
  Dim reader As New DataTableReader(GetStringTable())
  dataSet.Load(reader, LoadOption.OverwriteChanges, _
      AddressOf FillErrorHandler, table)

  Console.WriteLine("Press any key to continue.")
  Console.ReadKey()
End Sub

Private Sub FillErrorHandler(ByVal sender As Object, _
  ByVal e As FillErrorEventArgs)
  ' You can use the e.Errors value to determine exactly what
  ' went wrong.
  If e.Errors.GetType Is GetType(System.FormatException) Then
    Console.WriteLine("Error when attempting to update the value: {0}", _
      e.Values(0))
  End If

  ' Setting e.Continue to True tells the Load
  ' method to continue trying. Setting it to False
  ' indicates that an error has occurred, and the 
  ' Load method raises the exception that got 
  ' you here.
  e.Continue = True
End Sub

Private Function GetIntegerTable() As DataTable
  ' Create sample table with a single Int32 column.
  Dim table As New DataTable

  Dim idColumn As DataColumn = table.Columns.Add("ID", _
      GetType(Integer))

  ' Set the ID column as the primary key column.
  table.PrimaryKey = New DataColumn() {idColumn}

  table.Rows.Add(New Object() {4})
  table.Rows.Add(New Object() {5})
  table.TableName = "IntegerTable"
  table.AcceptChanges()
  Return table
End Function

Private Function GetStringTable() As DataTable
  ' Create sample table with a single String column.
  Dim table As New DataTable

  Dim idColumn As DataColumn = table.Columns.Add("ID", _
      GetType(String))

  ' Set the ID column as the primary key column.
  table.PrimaryKey = New DataColumn() {idColumn}

  table.Rows.Add(New Object() {"Mary"})
  table.Rows.Add(New Object() {"Andy"})
  table.Rows.Add(New Object() {"Peter"})
  table.AcceptChanges()
  Return table
End Function

Private Sub PrintColumns( _
   ByVal table As DataTable)

  ' Loop through all the rows in the DataTableReader.
  For Each row As DataRow In table.Rows
    For Each col As DataColumn In table.Columns
      Console.Write(row(col).ToString() & " ")
    Next
    Console.WriteLine()
  Next
End Sub

注釈

Load メソッドは、単一のDataTableにデータを入力する手法を提供し、IDataReader インスタンスから取得します。 このメソッドは同じ機能を提供しますが、DataSet内の複数のテーブルにIDataReaderから複数の結果セットを読み込むことができます。

受信readerのソース データ列のいずれかが計算列の場合、読み込み操作はInvalidOperationExceptionで失敗します。

loadOption パラメーターを使用すると、インポートしたデータを既存のデータとやり取りする方法を指定できます。また、LoadOption列挙体の任意の値を指定できます。 このパラメーターの使用方法の詳細については、 DataTableLoad メソッドのドキュメントを参照してください。

errorHandler パラメーターは、データの読み込み中にエラーが発生したときに呼び出されるプロシージャを参照するFillErrorEventHandler デリゲートです。 プロシージャに渡される FillErrorEventArgs パラメーターは、発生したエラー、現在のデータ行、および入力中の DataTable に関する情報を取得できるプロパティを提供します。 より単純な try/catch ブロックではなく、このデリゲート メカニズムを使用すると、エラーを特定し、状況を処理し、必要に応じて処理を続行できます。 FillErrorEventArgs パラメーターは、Continueプロパティを指定します。このプロパティをtrueに設定して、エラーを処理したことを示し、処理を続行することを示します。処理を停止することを示すプロパティをfalseに設定します。 プロパティを false に設定すると、問題をトリガーしたコードが例外をスローする原因になることに注意してください。

tables パラメーターを使用すると、リーダーから読み込まれる各結果セットに対応するテーブルの順序を示す、DataTable インスタンスの配列を指定できます。 Load メソッドは、指定された各DataTable インスタンスに、ソース データ リーダーの 1 つの結果セットのデータを入力します。 各結果セットの後、 Load メソッドは、結果セットがなくなったまで、リーダー内の次の結果セットに移動します。

このメソッドの名前解決スキームは、DbDataAdapter クラスの Fill メソッドと同じです。

こちらもご覧ください

適用対象