使用英语阅读

通过


DataSet.Load 方法

定义

通过所提供的 IDataReader,用某个数据源的值填充 DataSet

重载

Load(IDataReader, LoadOption, DataTable[])

使用提供的 DataSet 以数据源的值填充 IDataReader,同时使用 DataTable 实例的数组提供架构和命名空间信息。

Load(IDataReader, LoadOption, String[])

使用所提供的 DataSet,并使用字符串数组为 DataSet 中的表提供名称,从而用来自数据源的值填充 IDataReader

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

使用提供的 DataSet 以数据源的值填充 IDataReader,同时使用 DataTable 实例的数组提供架构和命名空间信息。

注解

方法Load提供了一种使用从 IDataReader 实例检索的数据填充单个DataTable的技术。 此方法提供相同的功能,但允许您将多个结果集从 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[])

Source:
DataSet.cs
Source:
DataSet.cs
Source:
DataSet.cs

使用提供的 DataSet 以数据源的值填充 IDataReader,同时使用 DataTable 实例的数组提供架构和命名空间信息。

public void Load (System.Data.IDataReader reader, System.Data.LoadOption loadOption, params System.Data.DataTable[] tables);

参数

reader
IDataReader

提供一个或多个结果集的 IDataReader

loadOption
LoadOption

一个来自 LoadOption 枚举的值,该值指示 DataTable 中的 DataSet 实例内已有的行如何与共享同一主键的传入行进行组合。

tables
DataTable[]

DataTable 实例的数组,Load(IDataReader, LoadOption, DataTable[]) 方法从该数组中检索名称和命名空间信息。 其中每个表都必须是此 DataTableCollection 所包含的 DataSet 的成员。

示例

以下示例创建一个新的 DataSet,将两 DataTable 个 实例添加到 DataSet,然后使用 方法填充 DataSetLoad ,从包含两个 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();
}

注解

方法Load提供了一种使用从 IDataReader 实例检索的数据填充单个DataTable的技术。 此方法提供相同的功能,但允许您将多个结果集从 IDataReader 加载到 中的 DataSet多个表中。

备注

如果传入reader中的任何源数据列是计算列,则加载操作将失败,并显示 InvalidOperationException

参数 loadOption 允许指定希望导入的数据如何与现有数据交互,并且可以是 枚举中的任何值 LoadOption 。 有关使用此参数的详细信息, DataTableLoad 请参阅 方法的文档。

参数 tables 允许您指定实例数组 DataTable ,指示与从读取器加载的每个结果集对应的表的顺序。 方法 Load 使用源数据读取器中单个结果集中的数据填充提供 DataTable 的每个实例。 在每个结果集之后, Load 该方法将转到读取器中的下一个结果集,直到没有更多结果集。

此方法的名称解析方案与后跟 Fill 类的 DbDataAdapter 方法相同。

另请参阅

适用于

Load(IDataReader, LoadOption, String[])

Source:
DataSet.cs
Source:
DataSet.cs
Source:
DataSet.cs

使用所提供的 DataSet,并使用字符串数组为 DataSet 中的表提供名称,从而用来自数据源的值填充 IDataReader

public void Load (System.Data.IDataReader reader, System.Data.LoadOption loadOption, params string[] tables);

参数

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

注解

方法Load提供了一种使用从 IDataReader 实例检索的数据填充单个DataTable的技术。 此方法提供相同的功能,但允许您将多个结果集从 IDataReader 加载到 中的 DataSet多个表中。

备注

如果传入reader中的任何源数据列是计算列,则加载操作将失败,并显示 InvalidOperationException

参数 loadOption 允许指定希望导入的数据如何与现有数据交互,并且可以是 枚举中的任何值 LoadOption 。 有关使用此参数的详细信息, Load 请参阅 方法的文档。

参数 tables 允许指定表名数组,指示从读取器加载的每个结果集所对应的表的顺序。 方法 Load 尝试按顺序在与表名数组中找到的名称匹配的 内 DataSet 查找表。 如果找到匹配的表,则会使用当前结果集的内容加载该表。 如果未找到匹配的表,则使用表名称数组中提供的名称创建一个表,并从结果集推断出新表的架构。 在每个结果集之后, Load 该方法将转到读取器中的下一个结果集,直到没有更多结果集。

DataSet关联的默认命名空间(如果有)与每个新创建的 DataTable相关联。 此方法的名称解析方案与后跟 Fill 类的 DbDataAdapter 方法相同。

另请参阅

适用于

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

Source:
DataSet.cs
Source:
DataSet.cs
Source:
DataSet.cs

使用提供的 DataSet 以数据源的值填充 IDataReader,同时使用 DataTable 实例的数组提供架构和命名空间信息。

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

参数

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;
}

注解

方法Load提供了一种使用从 IDataReader 实例检索的数据填充单个DataTable的技术。 此方法提供相同的功能,但允许您将多个结果集从 IDataReader 加载到 中的 DataSet多个表中。

备注

如果传入reader中的任何源数据列是计算列,则加载操作将失败,并显示 InvalidOperationException

参数 loadOption 允许指定希望导入的数据如何与现有数据交互,并且可以是 枚举中的任何值 LoadOption 。 有关使用此参数的详细信息, DataTableLoad 请参阅 方法的文档。

errorHandler参数是一个FillErrorEventHandler委托,它引用加载数据时发生错误时调用的过程。 FillErrorEventArgs传递给过程的参数提供属性,使你能够检索有关发生的错误、当前数据行和DataTable正在填充的 的信息。 使用此委托机制(而不是更简单的 try/catch 块)可以确定错误、处理情况,并根据需要继续处理。 参数 FillErrorEventArgs 提供属性 Continue :将此属性设置为 true 以指示已处理错误并希望继续处理;将 属性设置为 false 以指示希望停止处理。 请注意,将 属性设置为 false 会导致触发问题的代码引发异常。

参数 tables 允许您指定实例数组 DataTable ,指示与从读取器加载的每个结果集对应的表的顺序。 方法 Load 使用源数据读取器中单个结果集中的数据填充提供 DataTable 的每个实例。 在每个结果集之后, Load 该方法将转到读取器中的下一个结果集,直到没有更多结果集。

此方法的名称解析方案与后跟 Fill 类的 DbDataAdapter 方法相同。

另请参阅

适用于