Sdílet prostřednictvím


DataSet.Load Metoda

Definice

DataSet Vyplní hodnoty ze zdroje dat pomocí zadaného IDataReaderobjektu .

Přetížení

Name Description
Load(IDataReader, LoadOption, DataTable[])

DataSet Vyplní hodnoty ze zdroje dat pomocí zadaného IDataReaderpole DataTable instancí k zadání schématu a informací o oboru názvů.

Load(IDataReader, LoadOption, String[])

DataSet Vyplní hodnoty ze zdroje dat pomocí zadaného IDataReaderpole řetězců k zadání názvů tabulek v objektu DataSet.

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

DataSet Vyplní hodnoty ze zdroje dat pomocí zadaného IDataReaderpole DataTable instancí k zadání schématu a informací o oboru názvů.

Poznámky

Metoda Load poskytuje techniku pro vyplnění jednoho DataTable dat načteným z IDataReader instance. Tato metoda poskytuje stejné funkce, ale umožňuje načíst více sad výsledků z více IDataReader tabulek v rámci .DataSet

DataSet Pokud už řádky obsahují, příchozí data ze zdroje dat se sloučí s existujícími řádky.

Tuto Load metodu lze použít v několika běžných scénářích, které jsou zaměřené na získávání dat ze zadaného zdroje dat a jejich přidání do aktuálního kontejneru dat (v tomto případě a DataSet). Tyto scénáře popisují standardní použití , DataSetpopisující jeho chování při aktualizaci a sloučení.

Synchronizuje DataSet nebo aktualizuje s jedním primárním zdrojem dat. Sleduje DataSet změny, což umožňuje synchronizaci s primárním zdrojem dat. Kromě toho DataSet může přijímat přírůstková data z jednoho nebo více sekundárních zdrojů dat. Není DataSet zodpovědný za sledování změn, aby bylo možné synchronizaci se sekundárním zdrojem dat.

Vzhledem k těmto dvěma hypotetickým zdrojům dat bude uživatel pravděpodobně vyžadovat jedno z následujících chování:

  • Inicializace DataSet z primárního zdroje dat V tomto scénáři chce uživatel inicializovat prázdnou DataSet hodnotu s hodnotami z primárního zdroje dat. Obsah jedné nebo více datových tabulek se upraví. Později chce uživatel rozšířit změny zpět do primárního zdroje dat.

  • Zachovat změny a znovu synchronizovat z primárního zdroje dat. V tomto scénáři chce uživatel převzít DataSet vyplněný předchozí scénář a provést přírůstkovou synchronizaci s primárním zdrojem dat a zachovat změny provedené v souboru DataSet.

  • Přírůstkový datový kanál ze sekundárních zdrojů dat V tomto scénáři chce uživatel sloučit změny z jednoho nebo více sekundárních zdrojů dat a tyto změny rozšířit zpět do primárního zdroje dat.

Tato Load metoda umožňuje všechny tyto scénáře. Tato metoda umožňuje zadat parametr možnosti načtení, který označuje, jak řádky již v DataTable kombinaci s řádky jsou načteny. Následující tabulka popisuje tři možnosti načtení, které LoadOption poskytuje výčet. V každém případě popis označuje chování, když primární klíč řádku v příchozích datech odpovídá primárnímu klíči existujícího řádku.

Možnost načtení Description
PreserveChanges (výchozí) Aktualizuje původní verzi řádku hodnotou příchozího řádku.
OverwriteChanges Aktualizuje aktuální a původní verze řádku hodnotou příchozího řádku.
Upsert Aktualizuje aktuální verzi řádku hodnotou příchozího řádku.

Obecně platí, že PreserveChanges možnosti a OverwriteChanges možnosti jsou určené pro scénáře, ve kterých musí uživatel synchronizovat DataSet změny a změny s primárním zdrojem dat. Možnost Upsert usnadňuje agregaci změn z jednoho nebo více sekundárních zdrojů dat.

Load(IDataReader, LoadOption, DataTable[])

Zdroj:
DataSet.cs
Zdroj:
DataSet.cs
Zdroj:
DataSet.cs
Zdroj:
DataSet.cs
Zdroj:
DataSet.cs

DataSet Vyplní hodnoty ze zdroje dat pomocí zadaného IDataReaderpole DataTable instancí k zadání schématu a informací o oboru názvů.

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())

Parametry

reader
IDataReader

Poskytuje IDataReader jednu nebo více sad výsledků.

loadOption
LoadOption

Hodnota z výčtu LoadOption , která označuje, jak se řádky v instancích v DataTable rámci objektu DataSet budou kombinovat s příchozími řádky, které sdílejí stejný primární klíč.

tables
DataTable[]

Pole DataTable instancí, ze kterých Load(IDataReader, LoadOption, DataTable[]) metoda načte informace o názvu a oboru názvů. Každá z těchto tabulek musí být členem DataTableCollection této DataSettabulky .

Atributy

Příklady

Následující příklad vytvoří novou DataSet, přidá dvě DataTable instance do DataSeta pak vyplní DataSet pomocí Load metody načítání dat z DataTableReader obsahující dvě sady výsledků. Nakonec příklad zobrazí obsah tabulek v okně konzoly.

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

Poznámky

Metoda Load poskytuje techniku pro vyplnění jednoho DataTable dat načteným z IDataReader instance. Tato metoda poskytuje stejné funkce, ale umožňuje načíst více sad výsledků z více IDataReader tabulek v rámci .DataSet

Poznámka:

Operace načítání selže s případem InvalidOperationException , že některý ze zdrojových sloupců dat v příchozích reader sloupcích vypočítá.

Parametr loadOption umožňuje určit, jak mají importovaná data pracovat s existujícími daty, a může to být libovolná hodnota z výčtu LoadOption . Další informace o použití tohoto parametru najdete v dokumentaci k DataTableLoad metodě.

Parametr tables umožňuje zadat pole DataTable instancí, které určuje pořadí tabulek odpovídajících každé sadě výsledků načtené ze čtečky. Metoda Load vyplní každou zadanou DataTable instanci daty z jedné sady výsledků ze zdrojové čtečky dat. Po každé sadě výsledků se Load metoda přesune na další sadu výsledků v rámci čtenáře, dokud nebudou k dispozici žádné další sady výsledků.

Schéma překladu ip adres pro tuto metodu je stejné jako metoda, za kterou následuje Fill metoda DbDataAdapter třídy.

Viz také

Platí pro

Load(IDataReader, LoadOption, String[])

Zdroj:
DataSet.cs
Zdroj:
DataSet.cs
Zdroj:
DataSet.cs
Zdroj:
DataSet.cs
Zdroj:
DataSet.cs

DataSet Vyplní hodnoty ze zdroje dat pomocí zadaného IDataReaderpole řetězců k zadání názvů tabulek v objektu 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())

Parametry

reader
IDataReader

Poskytuje IDataReader jednu nebo více sad výsledků.

loadOption
LoadOption

Hodnota z výčtu LoadOption , která označuje, jak se řádky v instancích v DataTable rámci objektu DataSet budou kombinovat s příchozími řádky, které sdílejí stejný primární klíč.

tables
String[]

Pole řetězců, ze kterých Load metoda načte informace o názvu tabulky.

Atributy

Příklady

Následující příklad konzolové aplikace nejprve vytvoří tabulky a načte data ze čtenáře do , DataSetpomocí Load metody. Příklad pak přidá tabulky do DataSet a pokusí se vyplnit tabulky daty z objektu DataTableReader. V tomto příkladu Load , protože parametry předané metodě označují název tabulky, který neexistuje, vytvoří metoda novou tabulku, Load která bude odpovídat názvu předaného jako parametr. Po načtení dat se v příkladu zobrazí obsah všech jejích tabulek v okně konzoly.

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

Poznámky

Metoda Load poskytuje techniku pro vyplnění jednoho DataTable dat načteným z IDataReader instance. Tato metoda poskytuje stejné funkce, ale umožňuje načíst více sad výsledků z více IDataReader tabulek v rámci .DataSet

Poznámka:

Operace načítání selže s případem InvalidOperationException , že některý ze zdrojových sloupců dat v příchozích reader sloupcích vypočítá.

Parametr loadOption umožňuje určit, jak mají importovaná data pracovat s existujícími daty, a může to být libovolná hodnota z výčtu LoadOption . Další informace o použití tohoto parametru najdete v dokumentaci k Load metodě.

Parametr tables umožňuje zadat pole názvů tabulek označující pořadí tabulek odpovídajících každé sadě výsledků načtené ze čtečky. Metoda Load se pokusí najít tabulku v rámci DataSet odpovídajícího názvu nalezeného v poli názvů tabulek v pořadí. Pokud se najde odpovídající tabulka, tato tabulka se načte s obsahem aktuální sady výsledků. Pokud se nenajde žádná odpovídající tabulka, vytvoří se tabulka s použitím názvu zadaného v matici názvů tabulek a schéma nové tabulky se odvodí ze sady výsledků. Po každé sadě výsledků se Load metoda přesune na další sadu výsledků v rámci čtenáře, dokud nebudou k dispozici žádné další sady výsledků.

Výchozí obor názvů přidružený DataSetk , pokud existuje, je přidružen ke každému nově vytvořenému DataTable. Schéma překladu ip adres pro tuto metodu je stejné jako metoda, za kterou následuje Fill metoda DbDataAdapter třídy.

Viz také

Platí pro

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

Zdroj:
DataSet.cs
Zdroj:
DataSet.cs
Zdroj:
DataSet.cs
Zdroj:
DataSet.cs
Zdroj:
DataSet.cs

DataSet Vyplní hodnoty ze zdroje dat pomocí zadaného IDataReaderpole DataTable instancí k zadání schématu a informací o oboru názvů.

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())

Parametry

reader
IDataReader

Poskytuje IDataReader jednu nebo více sad výsledků.

loadOption
LoadOption

Hodnota z výčtu LoadOption , která označuje, jak se řádky v instancích v DataTable rámci objektu DataSet budou kombinovat s příchozími řádky, které sdílejí stejný primární klíč.

errorHandler
FillErrorEventHandler

FillErrorEventHandler Delegát, který volá, když dojde k chybě při načítání dat.

tables
DataTable[]

Pole DataTable instancí, ze kterých Load(IDataReader, LoadOption, FillErrorEventHandler, DataTable[]) metoda načte informace o názvu a oboru názvů.

Atributy

Příklady

Následující příklad přidá tabulku do objektu DataSeta pokusí se použít metodu Load pro načtení dat z DataTableReader nekompatibilního schématu. Místo zachycení chyby tento příklad používá FillErrorEventHandler delegáta k prozkoumání a zpracování chyby. Výstup se zobrazí v okně konzoly.

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

Poznámky

Metoda Load poskytuje techniku pro vyplnění jednoho DataTable dat načteným z IDataReader instance. Tato metoda poskytuje stejné funkce, ale umožňuje načíst více sad výsledků z více IDataReader tabulek v rámci .DataSet

Poznámka:

Operace načítání selže s případem InvalidOperationException , že některý ze zdrojových sloupců dat v příchozích reader sloupcích vypočítá.

Parametr loadOption umožňuje určit, jak mají importovaná data pracovat s existujícími daty, a může to být libovolná hodnota z výčtu LoadOption . Další informace o použití tohoto parametru najdete v dokumentaci k DataTableLoad metodě.

Parametr errorHandler je FillErrorEventHandler delegát, který odkazuje na proceduru, která se volá, když dojde k chybě při načítání dat. Parametr FillErrorEventArgs předaný postupu poskytuje vlastnosti, které umožňují načíst informace o chybě, ke které došlo, aktuálním řádku dat a DataTable vyplnění. Použití tohoto mechanismu delegáta místo jednoduššího bloku try/catch umožňuje určit chybu, zpracovat situaci a pokračovat ve zpracování, pokud chcete. Parametr FillErrorEventArgs poskytuje Continue vlastnost: nastavte tuto vlastnost tak, aby true indikovala, že jste zpracovali chybu a chcete pokračovat ve zpracování; nastavte vlastnost tak, aby false označovala, že chcete zastavit zpracování. Mějte na paměti, že nastavení vlastnosti způsobí false , že kód, který problém aktivoval vyvolá výjimku.

Parametr tables umožňuje zadat pole DataTable instancí, které určuje pořadí tabulek odpovídajících každé sadě výsledků načtené ze čtečky. Metoda Load vyplní každou zadanou DataTable instanci daty z jedné sady výsledků ze zdrojové čtečky dat. Po každé sadě výsledků se Load metoda přesune na další sadu výsledků v rámci čtenáře, dokud nebudou k dispozici žádné další sady výsledků.

Schéma překladu ip adres pro tuto metodu je stejné jako metoda, za kterou následuje Fill metoda DbDataAdapter třídy.

Viz také

Platí pro