DataSet.Load 方法

定義

使用所提供的 DataSet,用資料來源的值填滿 IDataReader

多載

Load(IDataReader, LoadOption, DataTable[])

使用所提供的 DataSet,以資料來源的值,填入 IDataReader,使用 DataTable 執行個體的陣列,以提供結構描述和命名空間資訊。

Load(IDataReader, LoadOption, String[])

使用所提供的 DataSet,以資料來源的值填入 IDataReader,使用字串的陣列來提供 DataSet 之內的資料表名稱。

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

使用所提供的 DataSet,以資料來源的值,填入 IDataReader,使用 DataTable 執行個體的陣列,以提供結構描述和命名空間資訊。

備註

方法 Load 提供一種技術,以從 實例擷取的資料填入單 DataTableIDataReader 。 這個方法提供相同的功能,但可讓您將多個結果集從 IDataReader 載入至 內的 DataSet 多個資料表。

如果 DataSet 已經包含資料列,從資料來源傳入的資料會與現有的資料列合併。

方法 Load 可用於數個常見案例中,全都以從指定的資料來源取得資料為中心,並將它新增至目前的資料容器 (,在此案例 DataSet 中為) 。 這些案例描述 的標準用法 DataSet ,描述其更新和合併行為。

DataSet 同步處理或更新與單一主要資料來源。 追蹤 DataSet 變更,允許與主要資料來源同步處理。 此外, DataSet 也可以接受來自一或多個次要資料來源的累加資料。 DataSet不負責追蹤變更,以便允許與次要資料來源進行同步處理。

假設這兩個假設資料來源,使用者可能需要下列其中一個行為:

  • 從主要資料來源初始化 DataSet 。 在此案例中,使用者想要使用來自主要資料來源的值初始化空 DataSet 的 。 會修改一或多個 DataTable 的內容。 稍後,使用者想要將變更傳播回主要資料來源。

  • 保留變更並從主要資料來源重新同步處理。 在此案例中,使用者想要擷 DataSet 取上一個案例中填入的 ,並執行與主要資料來源的累加同步處理,並保留 中 DataSet 所做的修改。

  • 來自次要資料來源的累加資料摘要。 在此案例中,使用者想要合併來自一或多個次要資料來源的變更,並將這些變更傳播回主要資料來源。

方法 Load 可讓所有這些案例都可行。 這個方法可讓您指定 load 選項參數,指出資料列 DataTable 如何結合所載入的資料列。 下表描述 列舉所提供的三個 LoadOption 載入選項。 在每個案例中,描述會指出傳入資料中資料列的主鍵符合現有資料列的主鍵時的行為。

載入選項 描述
PreserveChanges (預設) 使用傳入資料列的值來更新資料列的原始版本。
OverwriteChanges 使用傳入資料列的值來更新資料列的目前和原始版本。
Upsert 使用傳入資料列的值來更新資料列的目前版本。

一般而言, PreserveChangesOverwriteChanges 選項適用于使用者需要同步 DataSet 處理 和 其變更與主要資料來源的案例。 此選項 Upsert 有助於匯總一或多個次要資料來源的變更。

Load(IDataReader, LoadOption, DataTable[])

使用所提供的 DataSet,以資料來源的值,填入 IDataReader,使用 DataTable 執行個體的陣列,以提供結構描述和命名空間資訊。

public:
 void Load(System::Data::IDataReader ^ reader, System::Data::LoadOption loadOption, ... cli::array <System::Data::DataTable ^> ^ tables);
public void Load (System.Data.IDataReader reader, System.Data.LoadOption loadOption, params System.Data.DataTable[] tables);
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

IDataReader,提供一個或多個結果集。

loadOption
LoadOption

來自 LoadOption 列舉的值,表示已經在 DataTable 之內 DataSet 執行個體中的資料列如何與共用相同主索引鍵的傳入資料列結合。

tables
DataTable[]

DataTable 執行個體的陣列,Load(IDataReader, LoadOption, DataTable[]) 方法從中擷取名稱和命名空間資訊。 這些資料表的每一個都必須為這個 DataTableCollection 所包含 DataSet 的成員。

範例

下列範例會建立新的 DataSet 、將兩 DataTable 個實例加入 至 DataSet ,然後使用 方法填滿 DataSet Load ,從 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 提供一種技術,以從 實例擷取的資料填入單 DataTableIDataReader 。 這個方法提供相同的功能,但可讓您將多個結果集從 IDataReader 載入至 內的 DataSet 多個資料表。

注意

如果傳入 reader 中的任何來源資料行是計算資料行,載入作業將會失敗 InvalidOperationException

參數 loadOption 可讓您指定匯入資料與現有資料互動的方式,而且可以是列舉 LoadOption 中的任何值。 如需使用此參數的詳細資訊, DataTable Load 請參閱 方法的檔。

參數 tables 可讓您指定實例的 DataTable 陣列,指出對應至從讀取器載入之每個結果集的資料表順序。 方法會將 Load 每個提供的 DataTable 實例填入來自來源資料讀取器之單一結果集的資料。 在每個結果集之後, Load 方法會移至讀取器內的下一個結果集,直到沒有其他結果集為止。

這個方法的名稱解析配置與 後面接著 Fill 類別的 DbDataAdapter 方法相同。

另請參閱

適用於

Load(IDataReader, LoadOption, String[])

使用所提供的 DataSet,以資料來源的值填入 IDataReader,使用字串的陣列來提供 DataSet 之內的資料表名稱。

public:
 void Load(System::Data::IDataReader ^ reader, System::Data::LoadOption loadOption, ... cli::array <System::String ^> ^ tables);
public void Load (System.Data.IDataReader reader, System.Data.LoadOption loadOption, params string[] tables);
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

IDataReader,提供一個或多個結果集。

loadOption
LoadOption

來自 LoadOption 列舉的值,表示已經在 DataTable 之內 DataSet 執行個體中的資料列如何與共用相同主索引鍵的傳入資料列結合。

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 提供一種技術,以從 實例擷取的資料填入單 DataTableIDataReader 。 這個方法提供相同的功能,但可讓您將多個結果集從 IDataReader 載入至 內的 DataSet 多個資料表。

注意

如果傳入 reader 中的任何來源資料行是計算資料行,載入作業將會失敗 InvalidOperationException

參數 loadOption 可讓您指定匯入資料與現有資料互動的方式,而且可以是列舉 LoadOption 中的任何值。 如需使用此參數的詳細資訊, Load 請參閱 方法的檔。

參數 tables 可讓您指定資料表名稱的陣列,指出對應至從讀取器載入之每個結果集的資料表順序。 方法 Load 會依序嘗試在符合資料表名稱陣列中找到的名稱內 DataSet 尋找資料表。 如果找到相符的資料表,該資料表會以目前結果集的內容載入。 如果找不到相符的資料表,則會使用資料表名稱陣列中提供的名稱來建立資料表,並從結果集推斷新的資料表架構。 在每個結果集之後, Load 方法會移至讀取器內的下一個結果集,直到沒有其他結果集為止。

與 相關聯的 DataSet 預設命名空間,如果有的話,會與每個新建立 DataTable 的 相關聯。 這個方法的名稱解析配置與 後面接著 Fill 類別的 DbDataAdapter 方法相同。

另請參閱

適用於

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

使用所提供的 DataSet,以資料來源的值,填入 IDataReader,使用 DataTable 執行個體的陣列,以提供結構描述和命名空間資訊。

public:
 virtual void Load(System::Data::IDataReader ^ reader, System::Data::LoadOption loadOption, System::Data::FillErrorEventHandler ^ errorHandler, ... cli::array <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);
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

IDataReader,提供一個或多個結果集。

loadOption
LoadOption

來自 LoadOption 列舉的值,表示已經在 DataTable 之內 DataSet 執行個體中的資料列如何與共用相同主索引鍵的傳入資料列結合。

errorHandler
FillErrorEventHandler

載入資料時發生錯誤,要呼叫的 FillErrorEventHandler 委派。

tables
DataTable[]

DataTable 執行個體的陣列,Load(IDataReader, LoadOption, FillErrorEventHandler, 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 提供一種技術,以從 實例擷取的資料填入單 DataTableIDataReader 。 這個方法提供相同的功能,但可讓您將多個結果集從 IDataReader 載入至 內的 DataSet 多個資料表。

注意

如果傳入 reader 中的任何來源資料行是計算資料行,載入作業將會失敗 InvalidOperationException

參數 loadOption 可讓您指定匯入資料與現有資料互動的方式,而且可以是列舉 LoadOption 中的任何值。 如需使用此參數的詳細資訊, DataTable Load 請參閱 方法的檔。

參數 errorHandlerFillErrorEventHandler 委派,參考載入資料時發生錯誤時所呼叫的程式。 FillErrorEventArgs傳遞至程式的參數提供屬性,可讓您擷取所發生錯誤的相關資訊、目前的資料列,以及 DataTable 正在填滿的資訊。 使用此委派機制,而不是更簡單的 try/catch 區塊,可讓您判斷錯誤、處理情況,並視需要繼續處理。 參數 FillErrorEventArgs 提供 Continue 屬性:將此屬性設定為 true ,表示您已處理錯誤並想要繼續處理;將 屬性設定為 false ,表示您想要停止處理。 請注意,將 屬性設定為 false 會導致觸發問題的程式碼擲回例外狀況。

參數 tables 可讓您指定實例的 DataTable 陣列,指出對應至從讀取器載入之每個結果集的資料表順序。 方法會將 Load 每個提供的 DataTable 實例填入來自來源資料讀取器之單一結果集的資料。 在每個結果集之後, Load 方法會移至讀取器內的下一個結果集,直到沒有其他結果集為止。

這個方法的名稱解析配置與 後面接著 Fill 類別的 DbDataAdapter 方法相同。

另請參閱

適用於